최선의 접근 방식은 아닐 수 있지만 별도의 클래스와 함께 사용하고 one line
하고 텍스트를 얻기 위해 호출 .
내 수업은 다음과 같습니다.
import Foundation
import UIKit
enum AttributedTextsType {
case underlined
case bold
case boldUnderlined
}
class AttributedTexts {
private static func underlinedText(color: UIColor, size: CGFloat) -> [NSAttributedString.Key : Any] {
let attrs = [
NSAttributedString.Key.font : UIFont.systemFont(ofSize: size),
NSAttributedString.Key.foregroundColor : color,
NSAttributedString.Key.underlineStyle : 1] as [NSAttributedString.Key : Any]
return attrs
}
private static func getAttibute(type: AttributedTextsType, color: UIColor, size: CGFloat) -> [NSAttributedString.Key : Any] {
var attributes: [NSAttributedString.Key : Any]!
switch type {
case .underlined:
attributes = AttributedTexts.underlinedText(color: color, size: size)
break
case .bold:
attributes = AttributedTexts.underlinedText(color: color, size: size)
break
case .boldUnderlined:
attributes = AttributedTexts.underlinedText(color: color, size: size)
break
}
return attributes
}
static func set(string: String, color: UIColor, type: AttributedTextsType, size: CGFloat = 19.0) -> NSMutableAttributedString {
let attributes = getAttibute(type: type, color: color, size: size)
let attributedString = NSMutableAttributedString(string:"")
let buttonTitleStr = NSMutableAttributedString(string: string, attributes: attributes)
attributedString.append(buttonTitleStr)
return attributedString
}
}
용법 let attributedString = AttributedTexts.set(string: "Skip", color: .white, type: .underlined, size: 19.0)
친애하는