Alternative for drawInRect:withFont: in iOS7


#1

In case someone was confused about the deprecated method “drawinRect:withFont:”, here’s the alternative:

And this is just another thing, but in the book, the code that precedes “drawinRect:withFont:” is “[[UIColor blackColor] setFill]]”, which pretty much does nothing, like someone else mentioned.
I don’t know if it is true, but I have a feeling that it might have changed to text color (and that’s the method that kept showing up on stackoverflow for changing text colors, which never worked for me) in the previous versions of Xcode.

So here’s the alternative for that also:

[text drawInRect:textRect withAttributes:@{NSFontAttributeName: font, NSForegroundColorAttributeName: [UIColor anyColor]}];

I’m new to the programming arena like many of you guys, so I’m not sure if that NSForegroundColorAttributeName is what is used to change the text color in the first place, or I’m using something wrong but as a side effect the text color is being changed. I only found out after trying different attributes… so someone who’s more experienced, please let me know if that is a legit code.

Thanks guys!


#2

Thank you very much for posting this! I was messing up my head all day trying to figure out how to use the new iOS7 methods. I honestly tried for several hours searching the documentarys (thought it was a good Idea to get into reading those*) but didn’t find anything about {NSFontAttributeName: font}

Would you tell me how (if) you used the docs to find this?

*if I’m that bad at fixing such a small thing, I wonder what will happen if I ever encounter a real problem… :confused: :cry:

cu
Chris


#3
The above expression looks like a short hand for creating a dictionary with a key (NSFontAttributeName) and a corresponding value (font). Probably a new construct introduced in the latest and greatest version of Objective-C.

Try reading the programming topics for collections.

See Also: Creating Dictionaries , Literal Syntax @ [developer.apple.com/library/mac ... tions.html](https://developer.apple.com/library/mac/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/FoundationTypesandCollections/FoundationTypesandCollections.html)

[quote]if I'm that bad at fixing such a small thing, I wonder what will happen if I ever encounter a real problem...[/quote]
Learning to fly an aircraft takes a couple of years, and what you are learning is no different.

The above expression looks like a short hand for creating a dictionary with a key (NSFontAttributeName) and a corresponding value (font). Probably a new construct introduced in the latest and greatest version of Objective-C.

Try reading the programming topics for collections.

See Also: Creating Dictionaries , Literal Syntax @ developer.apple.com/library/mac … tions.html

Learning to fly an aircraft takes a couple of years, and what you are learning is no different.


#4

I looked up Google with drawInRect withFont deprecated, and some things showed up on stackoverflow.
And then I went about experimenting with different things :slight_smile:


#5

Thanks BossaGroove - your code is good for drawing the text.
I found I had to change the code for the textRect as well.
sizeWithFont: is depreciated so I used sizeWithAttributes:
(as the documentation says - This method returns fractional sizes; to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function.)
So added some additional code:

[code]CGSize rectSize = [text sizeWithAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:28]}];

CGFloat fWidth = ceilf(rectSize.width);
CGFloat fHeight = ceilf(rectSize.height);

CGRect textRect = CGRectMake(0, 0, fWidth, fHeight);[/code]

There are probably easier ways, but this seems to work.


#6

Good post! This really helped me out a lot! And, I learned a bit more about debugging my code. I couldn’t get the shadow to show up and found that I was drawing the string prior to setting up the shadow variables. Duh!

Thanks,
Seth