Swift Language
문자열에서 머리 글자의 UIImage 생성
수색…
소개
이것은 이니셜의 UIImage를 생성하는 클래스입니다. 해리포터는 HP 이미지를 생성합니다.
InitialsImageFactory
class InitialsImageFactory: NSObject {
class func imageWith(name: String?) -> UIImage? {
let frame = CGRect(x: 0, y: 0, width: 50, height: 50)
let nameLabel = UILabel(frame: frame)
nameLabel.textAlignment = .center
nameLabel.backgroundColor = .lightGray
nameLabel.textColor = .white
nameLabel.font = UIFont.boldSystemFont(ofSize: 20)
var initials = ""
if let initialsArray = name?.components(separatedBy: " ") {
if let firstWord = initialsArray.first {
if let firstLetter = firstWord.characters.first {
initials += String(firstLetter).capitalized
}
}
if initialsArray.count > 1, let lastWord = initialsArray.last {
if let lastLetter = lastWord.characters.first {
initials += String(lastLetter).capitalized
}
}
} else {
return nil
}
nameLabel.text = initials
UIGraphicsBeginImageContext(frame.size)
if let currentContext = UIGraphicsGetCurrentContext() {
nameLabel.layer.render(in: currentContext)
let nameImage = UIGraphicsGetImageFromCurrentImageContext()
return nameImage
}
return nil
}
}
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow