0

When I needed to determine rect wrapping my text

  let rect = NSString(string: name).boundingRect(with: CGSize(width: frame.width, height: 40), options:  NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 14)], context: nil) 

I ran into .usesLineFragmentOrigin string drawing option. So I did not understand what this is an option and even more why somewhere some people uses NSStringDrawingOptions.usesFontLeading.union(NSStringDrawingOptions.usesLineFragmentOrigin) instead. When I was searching for answers, I was directed to Confused by NSStringDrawingOptions item meaning discussion, so from the first answer i derived that this option appears also in NSAttributedString()s instance method draw(with:options:context:), and I got why do we need it there. But I did not got what its conception or intention in String's boundingRect function. Even more I tried to contemplate on how can i apply NSAttributedString()s instance method draw(with:options:context:). So we haw a NSAttributedString() class, it has a method draw(with:options:context:). I thought i can create attributed string, then call method draw(at: )and specify a rect. But nothing worked. Please if someone knows, how can I use NSAttributedString()s draw(with:options:context:) function? And what this is an option .usesLineFragmentOrigin form the point of boundRect's function and why somewhere some people uses NSStringDrawingOptions.usesFontLeading.union(NSStringDrawingOptions.usesLineFragmentOrigin) instead? Thank you!

Ninja
  • 309
  • 7
  • 26
  • 1
    Actually the [documentation](https://developer.apple.com/documentation/foundation/nsattributedstring/1529478-draw) describes exactly what it does. – vadian Apr 04 '18 at 11:53
  • please see my question again + method description says : " Draws the attributed string in the specified bounding rectangle using the provided options." Ok, I specified rectangle but it did not draw text( I can't get why?( – Ninja Apr 04 '18 at 11:59
  • 1
    Normally those methods are used in `drawRect` of a custom `NS/UIView` – vadian Apr 04 '18 at 12:03
  • Key words are `in the current graphics context` — have you opened graphic context? – user28434'mstep Apr 04 '18 at 12:36
  • @user28434 No i did not because method description said: " Draws the attributed string in the specified bounding rectangle using the provided options." Can you give me a clue on how to create it? But i need to use NSAttributedString()s draw(with:options:context:) not just uiviews draw in function – Ninja Apr 04 '18 at 12:46

0 Answers0