How to implement the large text accessibility to uilabel attributed without losing the font - ios

i have been trying to implement the large text accessibility for attributed text in a label. I have tried a lot of research and found the following way
attributedTextLabel.attributedText = viewModel.attributedString
// accessibility
attributedTextLabel.isAccessibilityElement = true
if let accessibilityLabel = viewModel.accessibilityLabel {
attributedTextLabel.accessibilityLabel = accessibilityLabel
attributedTextLabel.font = UIFont.preferredFont(forTextStyle: .body)
attributedTextLabel.adjustsFontForContentSizeCategory = true
}
But the issue with this approach is that iam loosing the font i have set for the attributed text which consists of multiple fonts in the format of :-
Bold: Simple text
Guys, is there any way to implement large text accessibility for uilabel dynamic text retaining the font of the attributed text. Thanks in advance

Related

Label text clips at bottom when using CustomFont in iOS

I am using Custom Font in iOS application (Xamarin.Forms App). It is working fine but the text is clipped at the bottom of UILabel. It clearly cuts letter "g"
Custom Font: FuturaStd-Light.ttf
Actual Image In App:
Edited Image to ensure not height issue: Added Background color to Label to ensure it is not height issue.
I read here that to adjust ascender and descender property of Font. But still it doesn't help. Any help would be really appreciated.
Note: It is looking good if we remove custom font. The issue occurs only for the custom font that i am using.
you could try to use this:
NSMutableParagraphStyle paragraphStyle = new NSMutableParagraphStyle();
paragraphStyle.MinimumLineHeight = 24 + 1;//24 is your font size
NSMutableDictionary attributes = new NSMutableDictionary();//NSParagraphStyleAttributeName
attributes.SetValueForKey(paragraphStyle,new NSString("NSParagraphStyleAttributeName"));
label.AttributedText = new NSMutableAttributedString(label.Text,attributes);

UILabel with attributedText in custom UITableViewCell: Color not rendering until reuse (iOS10)

I have a convenience extension like this:
extension NSMutableAttributedString {
func append(string: String, attributes: [String: Any]) {
let attributed = NSMutableAttributedString(string: string)
let range = NSRange(location: 0, length: string.utf16.count)
for (attribute, value) in attributes {
attributed.addAttribute(attribute, value: value, range: range)
}
append(attributed)
}
}
I'm styling text in my UILabel thusly:
let normalAttributes = [NSForegroundColorAttributeName: darkGray]
let lightAttributes = [NSForegroundColorAttributeName: lightGray]
let text = NSMutableAttributableString()
text.append("Part 1", attributes: normalAttributes)
text.append("Part 2", attributes: lightAttributes)
All of this within a custom UITableViewCell class. What's happening is that the text is rendering with the base color in the NIB file, rather than the custom foreground color as per my attributed string - until I do something (like scroll around) that causes cell reuse, after which suddenly the colors render correctly.
I can't seem to find anything wrong with the lifecycle of the object, and there's no other place that's messing with this label. The label, fwiw, is an IBOutlet, so I'm not creating a new one each time or anything strange.
Turns out it's the same problem as:
Attributed string in tableviewcell not showing bold text until the cell is dequeued and reloaded
So it's solved by setting the font & color to nil on the label before setting the attributeText. Annoying, but easy enough to do that work-around.
As #Kiril Savino suggested. Setting up Font & Color of UILabel to nil is do easy work-around to get it done.
But one more thing I noticed today is Default Font used from Storyboard or XIB.
It should be System Font from Storyboard or XIB to make this work.
If you have used other Custom Font, then it won't work.
So, make sure Default Font of UILabel should System Font.
Thank you.

How to fit all numbers on my display label? - Basic Calculator

I'm a beginner in Swift and Xcode. As my very first project without tutorials I try to do basic Calculator app. My goal is to make it look like original calculator app on iOS 9. Everything works, but I don't know how to fit all numbers on the screen. In Apple app, if there are more numbers, they're getting smaller. How to do that?
For UITextField:
You have to set the adjustFontSizeToFitWidth property to yes and provide a suitable small value for the minimumFontSize.
Both together will cause the UITextField to reduce its font size to fit the string in its bounds.
let textfield = UITextField()
textfield.adjustsFontSizeToFitWidth = true
textfield.minimumFontSize = 5
// ... more
For UILabel:
You have to set the adjustsFontSizeToFitWidth property to yes and provide a suitable small value for the minimumScaleFactor.
Both together will cause the UILabel to reduce its font size to fit the string in its bounds.
let label = UILabel()
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.05
// ... more

Selecting a word and shows tooltip in iOS

I would like to implement the Medium iOS App like effect for tapping highlight and shows tooltip.
I have been researching on Text Kit and some other stackoverflow questions have some thoughts on it, please also suggest what's the better alternative to this.
Scenario:
Static Text pre-defined
Shows highlights in several words or phrases
Solution thoughts:
Use UITextView for storing text
Use attributed string for text content
Showing background color using NSBackgroundColorAttributedName
Detect the selection by layoutManager.characterIndexForPoint(...)
Shows tooltip next to the selection
Shows tooltip using one of these pods AMPopTip, CMPopTipView, EasyTipView
Right now, I am not able to select the word and shows the tooltip just next to it. Any tier of help is appreciated.
Here is a way to Highlight text.
Create an IBOutlet named myLabel
In ViewDidLoad type
let yourString = "This is how you highlight text"
myLabel.text = yourString
let malleableString = NSMutableAttributedString(string: yourString)
let numberOfCharactersToHighlight = 5
let startingIndex = 1
malleableString.addAttribute(NSBackgroundColorAttributeName, value: .magenta, range: NSRange(location: startingIndex, length: numberOfCharactersToHighlight))
myLabel.attributedText = malleableString

iOS: set textview font to body programmatically

I know how to set font programmatically when it comes to things like system fonts or custom fonts. But how do I specify the body font style? or caption or headline, for that matter?
Here is one way, you can modify it for your need.
attributes:#{NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleBody]}
For the others, replace body with say headline, etc.
You can go for setting attributed string using NSAttributedString.
You alloc and init the NSAttributed string object set the attributes like body fonts and or headline etc.
Moreover, you can set the HTML text in the attributed text.
I hope the following links might help you...
Example of NSAttributedString with two different font sizes?
http://sketchytech.blogspot.in/2013/11/making-most-of-uitextview-in-ios-7.html
In Swift, you have to create a font with the class function called preferredFont from UIFont:
let label1 = UILabel()
label1.font = UIFont.preferredFont(forTextStyle: .body)
let label2 = UILabel()
label2.font = UIFont.preferredFont(forTextStyle: .caption1)
let label3 = UILabel()
label3.font = UIFont.preferredFont(forTextStyle: .headline)
You can find all the text styles here:
UIFont.TextStyle documentation

Resources