why is UIFont.LineHeight read-only? - ios

Any idea how to set line height in MonoTouch?
UIFont.LineHeight is read-only and has got no setter.
MonoTouch: 6.0.8

It's an Objective-C readonly property, see Apple documentation.
What you need to do is create a new UIFont instance, with a size that will match the line height you want, and assign (or use) that font instance in your code.

Some time ago I needed to change line spacing, but keep font size. Maybe this solution is a dirty hack, but it is simpler than writing your own UI components or use third-party libraries.

Related

iOS - Dynamic Type and Interface Builder

Trying to implement support for Dynamic Type and have an issue. I set the style I want to use on a label or something in Interface Builder. I register for the UIContentSizeCategoryDidChangeNotification, and in the handler, I set the label's font to ... what? How do I know what style to use? Shouldn't there be an accessor that lets me find that out? If not, I have to put it in 2 places, which means they'll get out of sync and I'll be annoyed. Any thoughts?
I don’t think this will satisfy you, but set the font to [UIFont preferredFontForTextStyle:UIFontTextStyleTitle2 or whatever style you set in Interface Builder.
Ignore the setting in Interface Builder. It’s not even worth setting. Interface Builder is a (mostly) static representation of the initial state of your views, but this is Dynamic Type.
You could subclass UILabel to make it dynamic, and/or join us on the dark side of setting up views in code.
Since iOS 10, there's no need to follow this rationale because the adjustsFontForContentSizeCategory property allows an automatic scaling of the font sizes according to the content type size selected in the settings.
All the text styles are well defined in the Apple reference site and their size variations as well.

How to make UITextView from scratch?

As I understand, Apple does not provide the source code for UIKit. In order to answer another question, though, I am trying to understand how UITextView works (or could be made to work) under the hood.
How would I set up a minimal UITextView myself?
I see from the documentation that it inherits from UIScrollView so I assume that I would start there.
import UIKit
class MyUITextView: UIScrollView {
// ???
}
Again looking at the text view docs, it looks like I would need to at a minimum implement the init method and the text property. (I can ignore all the editing aspects and formatting attributes for now.)
The init method takes the following form:
init(frame frame: CGRect, textContainer textContainer: NSTextContainer?)
So I would also need a property for an NSTextContainer. This TextKit component works together with NSTextStorage and NSLayoutManager so I need to work those in somewhere, too. I could set the NSTextStorage with the text property but I really don't know how NSLayoutManager would interact here.
Has anyone (outside of Apple) done this before? Is this a simple enough question to answer here or would the answer be to long?
Update:
This question shows my latest attempt: How to Initialize NSTextStorage with a String in Swift
This is definitely complicated. I've had to reimplement a subset of UILabel before, and that was tricky. I think the first thing you should think about is what level you're interested in working in. At it's most basic, UITextView is responsible for manipulating a bitmap graphics context and turning a string into pixels on your screen. That in itself is a pretty big challenge, and if you want to reimplement that functionality from scratch you're going to be busy for a while.
At a higher level, UITextView does things like breaking text up into lines, displaying different fonts; higher still and you have things like the UITextInput Protocol, which handles letting the user enter and manipulate the text view's contents.
In terms of implementation details, those obviously aren't available. The closest we can get is a header dump, which is interesting but might not tell us much.
Until iOS7 and TextKit, text rendering was actually handled by WebKit, which means the implementation is potentially more of a mess for having undergone that transition.
Anyway, some things to point you in the right (or at least a) direction:
NSAttributedString Class Reference, especially the UIKit Additions, which let you actually blit text into a graphics context;
Introducing TextKit, some sample code from WWDC13 (there's an accompanying video as well)
Intro to Text Kit from AppCoda.
I apologize that this answer probably isn't as useful as I'd intended. Basically: this is a really big question; it's probably multiple really big questions.
You can use the below sources to understand what happens under the hood. Apple'e implementation need not be the same though.
https://github.com/BigZaphod/Chameleon/blob/master/UIKit/Classes/UITextView.h
https://github.com/BigZaphod/Chameleon/blob/master/UIKit/Classes/UITextView.m

Custom font with size classes in iOS

I'm trying to add 2 different font sizes for iphone and ipad layouts using size classes. It works cool with a default System font but doesn't work with custom font(I'm using PragmataPro in my project). If I add the second size for wR hR then font looks correctly in interface builder(I even checked xml) but in simulator and on device it becomes System instead of PragmataPro. But if I remove wR hR(or whatever layout I'm using for another size) then font shows correctly. Any idea how to solve this issue?
Thanks!
Subclass UILabel and override "layoutSubviews" method like:
- (void)layoutSubviews
{
[super layoutSubviews];
// Implement font logic depending on screen size
self.font = [UIFont fontWithName:#"CustomFont" size:self.font.pointSize];
}
Follow the link (it is a step-by-step from Apple):
https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/ChangingtheFontforaSizeClass.html
The 'custom' is there to define one single setting per type class.
If you don't define a 'custom' library with all the variations you want than I don't really think there to be a solution, mainly because by 'custom' you mean ONE SINGLE COMPONENT OF A GIVEN LIST OF CHOICES...

Change System font for testing purposes

I'm currently working on an iOS project which has tons of labels/buttons/controls spread over dozens of scenes.
Most of those controls were created using Interface Builder.
So, it's now my job to make sure that every control (especially the labels) is formatted with the correct font family, which is not the case at the moment because many devs simply forget to change the font (our font must be set programmatically since it's not included in IB) after adding the control in IB.
Is there a way to change the system font temporarily so it's easier to see where font-settings have been forgotten?
I've searched for:
Changing the system font programmatically
Changing the font in Xcode somewhere
Changing the font in the iOS simulator (maybe as a debugging option)
But I was unsuccessful so far. I can't be the only one with this kind of problem - it's just naturally tedious to set every single control font programmatically.
The only thing I could imagine is like overriding the base UILabel's drawing method with a custom font (wingdings anyone?), but that seems a bit excessive?
You can try creating a category, which overrides the systemFontOfSize: method of UIFont or use method swizzling (you can find out more about method swizzling here: http://cocoadev.com/MethodSwizzling). Both are extremely ugly and shouldn't be used in production, but should be fine for testing purposes.
Here is an example category of UIFont:
#interface UIFont (SysFont)
#end
#implementation UIFont (SysFont)
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize {
return [UIFont fontWithName:#"YourFont" size:fontSize];
}
#end

Where to set translatesAutoresizingMaskIntoConstraints in Xcode 4.5

I need to set translatesAutoresizingMaskIntoConstraints to NO. By default it is set to YES (to assist with the majority of apps that are transitioning from struts and springs to the new Auto Layout).
Is there somewhere in Xcode where the default can be changed from YES to NO?
Or do I have to manually set it for every view?
I'm late to this question, but the mentioned option is still missing in Xcode 5 & 6, so the question is still meaningful.
Actually, we can always set a value to any property of a view/control/object by adding a User Defined Runtime Atribute in Storyboard (Interface Builder) like the following screenshot.
And it also works for translatesAutoresizingMaskIntoConstraints. So the question could be solved.
This is a great question - and one I've tried to find an answer to myself. Sadly, it looks like there is no "quick fix". Currently, Apple considers Constraint-based layout Opt-in - even naming a section of the UIView Class Reference:
Opting in to Constraint-Based Layout
But that Opt-in is not global. I presume this is because not everything looks good if you just turn Springs & Struts into Constraints. Some UI elements break, or you would get a ton of unsatisfiable constraints errors.
I can think of one possible solution - I have not tried it myself, but you could make a category on UIView that sets all UIView objects to return NO for - (BOOL)translatesAutoresizingMaskIntoConstraints. While I do not know what this would break, it would globally set translatesAutoresizingMaskIntoConstraints to NO.
Here is a good introduction to Categories if you want to learn more about them!
When u have to change the size or position of your subview. Use (BOOL)translatesAutoresizingMaskIntoConstraints method before you set the frame of your subview.
[self.benchmarkButton removeFromSuperview];
[self.benchmarkButton setTranslatesAutoresizingMaskIntoConstraints:YES];
[self.benchmarkButton setFrame:CGRectMake(20, self.benchmarkButton.frame.origin.y+40, 260, 30)];
[self.benchmarksView addSubview:self.benchmarkButton];
Thats way your subview will not fight from constraints as it is default (AutoLayout) in Xcode 4.3 and later. Thanks
According to the documentation, this property is automatically set to NO if the view is added through Interface Builder.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instp/UIView/translatesAutoresizingMaskIntoConstraints

Resources