lingochamp/FuriganaTextView

How to set the text size?

lvcloudgit opened this issue · 6 comments

Thanks for offering, it helps me a lot.
Can you tell me how to set other properties? Like the font size or colors...
I'm sorry I'm a new coder, the original code is a bit too difficult for meto read!

Hi @lvcloudgit , I'm glad it's useful.

As the TextView takes a NSAttributedString to display, you can use the NSFontAttributeName to set a font (with the size you want) for a certain range.

In the demo we were doing something like this:

contents.appendAttributedString(NSAttributedString(string: "すみません。靴売り場はどこですか。\n\n", attributes: [NSFontAttributeName : exampleFontSansSerif]))

// ...

private var exampleFontSansSerif: UIFont {
    let fontDescriptor = UIFontDescriptor(name: "Hiragino Kaku Gothic ProN", size: 24)
    return UIFont(descriptor: fontDescriptor, size: 24)
}

I hope this information can help, thanks.

Really appreciate for reply! I just checked the Demo and learned a lot.
One more question please. I think a FuriganaTextView is a UIView contains a TextView? Because my FuriganaTextView is a dynamic value, and I want it always displayed in the center of the UIView, how can I make it? Thank you!

Sorry, this is my code to add a Furigana in a UIView, hwo can I set the Furigana in center of its container?

The code and the result display screenshot:

let furi = FuriganaTextView()
        var furiganas:[Furigana] = Array()

        if wordData.objectForKey("furigana") != nil {
            let furiganaData = wordData.objectForKey("furigana") as! NSDictionary
            for (key, data) in furiganaData {
                furiganas.append(
                    Furigana(
                        text: data.objectForKey("furigana") as! String,
                        original: key as! String,
                        range: NSMakeRange(data.objectForKey("position") as! Int, data.objectForKey("range") as! Int)
                        )
                )
            }

            let word = NSMutableAttributedString(string: theWord, attributes: [NSFontAttributeName: wordFont])
            furi.furiganas = furiganas
            furi.contents = word

        } else {
            // TODO
        }
       furi.frame = CGRectMake(0, 0, wordBox.bounds.width, wordBox.frame.height)
        wordBox.addSubview(furi)

2016-09-13 6 56 46

Hi, @lvcloudgit , I believe this will center the contents horizontally:

// ...

furi.alignment = .center
furi.furiganas = furiganas
furi.contents = word

// ...

If you need to center the contents vertically, you will have to calculate the size of the text view, then calculate the correct origin.y when it is centered vertically.

Also, I saw you are setting the text view's frame directly. You may want to checkout auto layout, which is kind of a more modern way to do the layout generally (by the way, FuriganaTextView also supports auto layout) .

Thank you for your help, all problems solved!

You are welcome 😉