gpt4 book ai didi

swift - Swift 中的上标字符串

转载 作者:行者123 更新时间:2023-11-28 14:52:01 27 4
gpt4 key购买 nike

我试图让我的文本在我的应用程序中对用户来说更易读。我正在从“https://api.myjson.com/bins/ss5jb”的 JSON 中读取我的文本。有没有办法在我的 collectionView 枚举字符串中为 verseNumber 添加上标并将其颜色更改为浅灰色?

枚举字符串是配对 verseNumber 和 verse 的最佳方式吗?

import UIKit

class PageCell: UICollectionViewCell {
let textLabel: UITextView = {
let label = UITextView()
//Custom Font//
guard let customFont = UIFont(name: "CormorantGaramond-Medium", size: 20) else {
fatalError("""
Failed to load the "RCormorantGaramond-Medium" font.
Make sure the font file is included in the project and the font name is spelled correctly.
"""
)
}
//End Custom Font//
label.font = customFont
label.text = ""
label.translatesAutoresizingMaskIntoConstraints = false
label.isEditable = false
label.isUserInteractionEnabled = false
return label
}()

override init(frame: CGRect) {
super.init(frame: frame)

addSubview(textLabel)

//textLabel Constraints
textLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 40).isActive = true
textLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
textLabel.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -15).isActive = true
textLabel.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 30).isActive = true

}
}




override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let pageCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId2", for: indexPath) as! PageCell
let page = book?.pages[indexPath.item]

if let page = page {
let enumeratedPage = page.text.enumerated()
pageCell.textLabel.text = enumeratedPage.reduce("") { (result: String, textPair) -> String in

let result = "\(result)\n"
let verseNumber = "\(textPair.offset + 1) "
let verse = "\(textPair.element)"

return result + verseNumber + verse
}
}

return pageCell
}

最佳答案

有两种方式:

方式 1:- NSAttributed 字符串

let mainText = "Here is a example of attributedString"
let attributeText = "attributedString"
let range = (mainText as NSString).range(of: attributeText)
let attributedString = NSMutableAttributedString(string:mainText)

attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: range)
attribute.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14) , range: range)

lblTitle.attributedText = attributedString

方式:- 2 您可以使用 HTML 属性:

 let htmlString = "<font color=\"red\">This is  </font> <font color=\"blue\"> some text!</font>"

let encodedData = htmlString.data(using: String.Encoding.utf8)!
let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
do {
let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
label.attributedText = attributedString

} catch _ {
print("Cannot create attributed String")
}

关于swift - Swift 中的上标字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49785718/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com