uiLabel 的下划线部分和 link 下划线部分
underline part of uiLabel and link underlined section
我是 iOS 开发新手。
我有一个标签 LatestInfo
,它有文本并且意味着 link 到网站:例如For the latest information visit example.com/latestInfo
我希望显示在 url example.com/latestInfo
下划线并使其可点击。
我使用的是 Swift 而不是 Objective-C
我该怎么做?
根据皮埃尔的要求进行编辑:
@IBOutlet weak var linkLabel: UITextView!
let string = "A great link : Google"
let range = (string as NSString).rangeOfString("Google")
let attributedString = NSMutableAttributedString(string: string)
attributedString.addAttribute(NSLinkAttributeName, value: NSURL("http://www.google.fr")!, range: range)
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(int: 1), range: range)
attributedString.addAttribute(NSUnderlineColorAttributeName, value: UIColor.orangeColor(), range: range)
linkLabel.attributedText = attributedString
寻找 NSMutableAttributedString,尤其是 NSLinkAttributeName。有很多关于此的教程和 Whosebug 问题。您还可以阅读 Apple 关于属性字符串的文档
TextView 是唯一能够打开链接的组件。因此,只需将您的标签替换为该标签即可:
let string = "A great link : Google"
let range = (string as NSString).rangeOfString("Google")
let attributedString = NSMutableAttributedString(string: string)
attributedString.addAttribute(NSLinkAttributeName, value: NSURL("http://www.google.fr")!, range: range)
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(int: 1), range: range)
attributedString.addAttribute(NSUnderlineColorAttributeName, value: UIColor.orangeColor(), range: range)
textView.attributedText = attributedString
Please create one UILabel & check it's properties.
Please select Text on first changed it's to plain to Attributed.
Now you can seen you label text in one Textfield. select that text & right click to you mouse & goto Font menu. you can seen Underline. select it. you can seen underline in your Label.
对于swift3.0
override func viewDidLoad() {
super.viewDidLoad()
let linkAttributes = [
NSLinkAttributeName: NSURL(string: "http://stalwartitsolution.co.in/luminutri_flow/terms-condition")!
] as [String : Any]
let attributedString = NSMutableAttributedString(string: "Please tick box to confirm you agree to our Terms & Conditions, Privacy Policy, Disclaimer. ")
attributedString.setAttributes(linkAttributes, range: NSMakeRange(44, 18))
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(value: 1), range: NSMakeRange(44, 18))
textview.delegate = self
textview.attributedText = attributedString
textview.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.red]
textview.textColor = UIColor.white
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
return true
}
如果你只想实现标签的一半作为下划线,你也可以使用它:-
对于Swift 4.0+
let attributesForUnderLine: [NSAttributedString.Key: Any] = [
.font: UIFont(name: AppFont.sourceSansPro_Regular, size: 12) ?? UIFont.systemFont(ofSize: 11),
.foregroundColor: UIColor.blue,
.underlineStyle: NSUnderlineStyle.single.rawValue]
let attributesForNormalText: [NSAttributedString.Key: Any] = [
.font: UIFont(name: AppFont.sourceSansPro_Regular, size: 12) ?? UIFont.systemFont(ofSize: 11),
.foregroundColor: AppColors.ColorText_787878]
let textToSet = "Want to change your preferences? Edit Now"
let rangeOfUnderLine = (textToSet as NSString).range(of: "Edit Now")
let rangeOfNormalText = (textToSet as NSString).range(of: "Want to change your preferences?")
let attributedText = NSMutableAttributedString(string: textToSet)
attributedText.addAttributes(attributesForUnderLine, range: rangeOfUnderLine)
attributedText.addAttributes(attributesForNormalText, range: rangeOfNormalText)
yourLabel.attributedText = attributedText
我是 iOS 开发新手。
我有一个标签 LatestInfo
,它有文本并且意味着 link 到网站:例如For the latest information visit example.com/latestInfo
我希望显示在 url example.com/latestInfo
下划线并使其可点击。
我使用的是 Swift 而不是 Objective-C
我该怎么做?
根据皮埃尔的要求进行编辑:
@IBOutlet weak var linkLabel: UITextView!
let string = "A great link : Google"
let range = (string as NSString).rangeOfString("Google")
let attributedString = NSMutableAttributedString(string: string)
attributedString.addAttribute(NSLinkAttributeName, value: NSURL("http://www.google.fr")!, range: range)
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(int: 1), range: range)
attributedString.addAttribute(NSUnderlineColorAttributeName, value: UIColor.orangeColor(), range: range)
linkLabel.attributedText = attributedString
寻找 NSMutableAttributedString,尤其是 NSLinkAttributeName。有很多关于此的教程和 Whosebug 问题。您还可以阅读 Apple 关于属性字符串的文档 TextView 是唯一能够打开链接的组件。因此,只需将您的标签替换为该标签即可:
let string = "A great link : Google"
let range = (string as NSString).rangeOfString("Google")
let attributedString = NSMutableAttributedString(string: string)
attributedString.addAttribute(NSLinkAttributeName, value: NSURL("http://www.google.fr")!, range: range)
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(int: 1), range: range)
attributedString.addAttribute(NSUnderlineColorAttributeName, value: UIColor.orangeColor(), range: range)
textView.attributedText = attributedString
Please create one UILabel & check it's properties.
Please select Text on first changed it's to plain to Attributed.
Now you can seen you label text in one Textfield. select that text & right click to you mouse & goto Font menu. you can seen Underline. select it. you can seen underline in your Label.
对于swift3.0
override func viewDidLoad() {
super.viewDidLoad()
let linkAttributes = [
NSLinkAttributeName: NSURL(string: "http://stalwartitsolution.co.in/luminutri_flow/terms-condition")!
] as [String : Any]
let attributedString = NSMutableAttributedString(string: "Please tick box to confirm you agree to our Terms & Conditions, Privacy Policy, Disclaimer. ")
attributedString.setAttributes(linkAttributes, range: NSMakeRange(44, 18))
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(value: 1), range: NSMakeRange(44, 18))
textview.delegate = self
textview.attributedText = attributedString
textview.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.red]
textview.textColor = UIColor.white
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
return true
}
如果你只想实现标签的一半作为下划线,你也可以使用它:-
对于Swift 4.0+
let attributesForUnderLine: [NSAttributedString.Key: Any] = [
.font: UIFont(name: AppFont.sourceSansPro_Regular, size: 12) ?? UIFont.systemFont(ofSize: 11),
.foregroundColor: UIColor.blue,
.underlineStyle: NSUnderlineStyle.single.rawValue]
let attributesForNormalText: [NSAttributedString.Key: Any] = [
.font: UIFont(name: AppFont.sourceSansPro_Regular, size: 12) ?? UIFont.systemFont(ofSize: 11),
.foregroundColor: AppColors.ColorText_787878]
let textToSet = "Want to change your preferences? Edit Now"
let rangeOfUnderLine = (textToSet as NSString).range(of: "Edit Now")
let rangeOfNormalText = (textToSet as NSString).range(of: "Want to change your preferences?")
let attributedText = NSMutableAttributedString(string: textToSet)
attributedText.addAttributes(attributesForUnderLine, range: rangeOfUnderLine)
attributedText.addAttributes(attributesForNormalText, range: rangeOfNormalText)
yourLabel.attributedText = attributedText