Waseem Mehany
Waseem Mehany

Reputation: 13

Superscript string in Swift

I'm trying to make my text more readable to the user in my app. I'm reading my text from a JSON from "https://api.myjson.com/bins/ss5jb". Is there a way to superscript the verseNumber and change its color to a light gray in my collectionView enumerated string?

And is an enumerated string the best way to pair the verseNumber and 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
    }

Upvotes: 1

Views: 2081

Answers (2)

Abhirajsinh Thakore
Abhirajsinh Thakore

Reputation: 1822

There are Two Ways:

Way 1:- NSAttributed String

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

Way :- 2 You can use HTML Property:

 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")
}

Upvotes: 1

Gereon
Gereon

Reputation: 17872

Use NSAttributedStrings for this. You can change text colors, baseline offsets and fonts to achieve your desired effect.

Upvotes: 1

Related Questions