我是iOS开发的新手。
我有一个标签LatestInfo
,它有文字,并且有一个指向网站的链接:例如For the latest information visit example.com/latestInfo
我希望显示屏为网址example.com/latestInfo
添加下划线并使其可点击。
我使用的是Swift而不是Obejective-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
答案 0 :(得分:14)
查找NSMutableAttributedString,尤其是NSLinkAttributeName。有很多关于这方面的教程和Stackoverflow问题。您还可以阅读Apple关于属性字符串的文档 TextView是唯一能够打开链接的组件。所以只需用你的标签替换你的标签:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key match="position" name="caseMatch" use="tag1"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<channel>
<xsl:for-each select="//position[count(. | key('caseMatch', tag1)[1]) = 1]">
<position>
<xsl:copy-of select="positionseqno"/>
<xsl:copy-of select="tag1"/>
<xsl:for-each select="key('caseMatch', tag1)">
<xsl:copy-of select="*[local-name()!='positionseqno' and local-name()!='tag1']" />
</xsl:for-each>
</position>
</xsl:for-each>
</channel>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:3)
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.
答案 2 :(得分:0)
对于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
}
答案 3 :(得分:0)
如果您只希望将标签的一半作为下划线,则也可以使用此选项:-
对于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