亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

快速使用屬性字符串使文本加粗

快速使用屬性字符串使文本加粗

iOS
人到中年有點甜 2019-11-12 10:59:18
我有這樣的字符串var str = "@text1 this is good @text1"現在,text1用另一個字符串代替t 1。我可以替換文本,但不能將其加粗。我想將新字符串加粗t 1,這樣最終輸出將是:@t 1這很好@t 1我該怎么做?我看到的所有示例都在Objective-C中,但是我想在Swift中完成。提前致謝。
查看完整描述

3 回答

?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

如果您使用的是本地化字符串,則可能無法始終依靠粗體字符串作為句子的結尾。如果是這種情況,則可以很好地進行以下操作:


例如查詢“ blah”與任何項目都不匹配


/* Create the search query part of the text, e.g. "blah". 

   The variable 'text' is just the value entered by  the user. */

let searchQuery = "\"\(text)\""


/* Put the search text into the message */

let message = "Query \(searchQuery). does not match any items"


/* Find the position of the search string. Cast to NSString as we want

   range to be of type NSRange, not Swift's Range<Index> */

let range = (message as NSString).rangeOfString(searchQuery)


/* Make the text at the given range bold. Rather than hard-coding a text size,

   Use the text size configured in Interface Builder. */

let attributedString = NSMutableAttributedString(string: message)

attributedString.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFontOfSize(label.font.pointSize), range: range)


/* Put the text in a label */

label.attributedText = attributedString


查看完整回答
反對 回復 2019-11-12
  • 3 回答
  • 0 關注
  • 634 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號