Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- combine
- clean architecture
- MVVM
- UICollectionView
- Clean Code
- 리팩토링
- ribs
- uitableview
- uiscrollview
- HIG
- swift documentation
- swiftUI
- ios
- tableView
- UITextView
- rxswift
- Refactoring
- 클린 코드
- 애니메이션
- collectionview
- 리펙토링
- SWIFT
- RxCocoa
- 스위프트
- map
- Human interface guide
- Observable
- Protocol
- Xcode
- 리펙터링
Archives
- Today
- Total
김종권의 iOS 앱 개발 알아가기
[iOS - swift] UIImage의 중앙에 text 추가하기 본문
UIGraphicsEndImageContext사용
- UIImageView을 extension하여 구현
func createImageWithLabelOverlay(text: String, isFromCamera: Bool = false) -> UIImage? {
let imageSize = self.image?.size ?? .zero
UIGraphicsBeginImageContextWithOptions(CGSize(width: imageSize.width, height: imageSize.height), false, 1.0)
let currentView = UIView(frame: CGRect(x: 0, y: 0, width: imageSize.width, height: imageSize.height))
let currentImage = UIImageView(image: self.image)
currentImage.frame = CGRect(x: 0, y: 0, width: imageSize.width, height: imageSize.height)
currentView.addSubview(currentImage)
let label = UILabel()
label.frame = currentView.frame
// for family in UIFont.familyNames {
// print("family:", family)
// for font in UIFont.fontNames(forFamilyName: family) {
// print("font:", font)
// }
// }
let fontSize: CGFloat = isFromCamera ? 100 : 34
let font = UIFont(name:"Noteworthy-Light" , size: fontSize)
let attributedStr = NSMutableAttributedString(string: text)
attributedStr.addAttribute(NSAttributedString.Key(rawValue: kCTFontAttributeName as String), value: font ?? .init(), range: (text as NSString).range(of: text))
attributedStr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.white, range: (text as NSString).range(of: text))
label.attributedText = attributedStr
label.numberOfLines = 0
label.textAlignment = .center
label.text = text
label.center = currentView.center
currentView.addSubview(label)
guard let currentContext = UIGraphicsGetCurrentContext() else {
return nil
}
currentView.layer.render(in: currentContext)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
image = img
return img
}
'iOS 응용 (swift)' 카테고리의 다른 글
[iOS - swift] Codable과 enum을 사용하여 특정 json 문자열 파싱 (0) | 2021.01.07 |
---|---|
[iOS - swift] OTPFieldView, 인증코드 입력창 (0) | 2021.01.06 |
[iOS - swift] 정규식 (Regex) (0) | 2021.01.05 |
[iOS - swift] SideMenu 사용 방법 (SideMenu 프레임워크) (0) | 2020.12.19 |
[iOS - swift] BaseViewController (Reachability, toast, dialog, loading - JGProgressHUD, setting, back pressed) (0) | 2020.12.19 |
Comments