9

Is it possible to set exclusion paths on a UILabel? The apple documentation states that UILabel is now built upon TextKit but a UILabel does not seem to expose a textContainer property.

I am looking to implement a tableview cell which includes multiple labels and a roux badge which the labels should not overlap (shown below).

enter image description here

If it is not possible with a UILabel how can I make a TextView act like a UILabel (no scroll, selection etc) and size it to fit it's contents?

bencallis
  • 3,478
  • 4
  • 32
  • 58

1 Answers1

8

It is correct that UILabel does not expose the TextKit stack, so you can't set an exclusion path.

However, you don't really need UILabel; it is easy to draw directly with TextKit into a rectangular graphics context, and now you can set an exclusion path (because you are drawing with TextKit). To draw with TextKit, just build the text kit "stack", customizing in any way you like, and then, when it's time to draw, call the layout manager's drawBackgroundForGlyphRange:... and drawGlyphsForGlyphRange:....

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Here's an example where I draw directly into a graphics context (in a UIView subclass's `drawRect:` with TextKit: https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch10p543drawingWithTextKit/ch23p815attributedStringDrawing3/StyledText.m – matt Mar 22 '14 at 18:04
  • If I went this route would I have to calculate the height of the cell manually in HeightForRow? Currently I use systemLayoutSizeFittingSize:UILayoutFittingCompressedSize similar to this http://blog.jldagon.me/blog/2013/12/07/auto-layout-and-uitableview-cells/ – bencallis Mar 22 '14 at 18:20
  • Yes, the layout manager tells you the height too. It does everything! – matt Mar 22 '14 at 18:53
  • thanks for the help. Is it possible to set a minimum fontSize/Scale and a maximum number of lines this way? – bencallis Jun 14 '14 at 11:04
  • 2
    @matt Your example is a 404 :( – Nathaniel Sep 08 '16 at 15:13
  • @Nathanial that was March of 2014. I'm sure you can find the example if you think about it. – matt Sep 08 '16 at 15:30
  • 1
    @Nathaniel https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch10p543drawingWithTextKit/ch23p815attributedStringDrawing3/StyledText.swift would appear to be an updated version. – aranasaurus Feb 08 '17 at 21:28