{"id":295697,"date":"2019-12-17T16:24:20","date_gmt":"2019-12-17T16:24:20","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=295697"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=295697","title":{"rendered":"\u041a\u0430\u043a \u043d\u0430\u0443\u0447\u0438\u0442\u044c UITextView \u043a\u0440\u0430\u0441\u0438\u0432\u043e \u0432\u044b\u0434\u0435\u043b\u044f\u0442\u044c\u0441\u044f"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/479992\/\">\n<h4>\u041f\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0438<\/h4>\n<p>  \u0414\u0430\u043d\u043e: \u043c\u043d\u043e\u0433\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442. <br \/>  \u041d\u0430\u0439\u0442\u0438: \u043a\u0440\u0430\u0441\u0438\u0432\u043e \u043e\u0444\u043e\u0440\u043c\u043b\u0435\u043d\u043d\u044b\u0439 \u0444\u043e\u043d.<\/p>\n<p>  \u00ab\u0414\u0430 \u044d\u0442\u043e \u0436\u0435 \u043d\u0430 \u0447\u0430\u0441\u043e\u043a\u00bb, \u2014 \u043f\u043e\u0434\u0443\u043c\u0430\u043b \u044f. \u2014 \u041d\u0443\u0436\u043d\u043e \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c backgroundColor \u0432 attributedText\u00bb. \u041d\u043e \u044d\u0442\u043e\u0433\u043e \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e. \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0435 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u2014 \u044d\u0442\u043e \u0437\u0430\u043a\u0440\u0430\u0448\u0435\u043d\u043d\u044b\u0439 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a. \u041d\u0435\u043a\u0440\u0430\u0441\u0438\u0432\u043e. \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u2014 \u043d\u0443\u0436\u0435\u043d \u043a\u0430\u0441\u0442\u043e\u043c! \u0422\u0435\u0440\u043d\u0438\u0441\u0442\u044b\u0439 \u043f\u0443\u0442\u044c \u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u0430\u0442\u043e\u043b\u043a\u043d\u0443\u043b \u043c\u0435\u043d\u044f \u043d\u0430 \u043c\u044b\u0441\u043b\u044c \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u0434\u0430\u0431\u044b \u0431\u0443\u0434\u0443\u0449\u0438\u043c \u043f\u043e\u043a\u043e\u043b\u0435\u043d\u0438\u044f\u043c \u043d\u0435 \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u0430\u0434\u0430\u0442\u044c. \u0417\u0430\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043e\u0432\u0430\u0432\u0448\u0438\u0445\u0441\u044f \u043f\u0440\u043e\u0448\u0443 \u043f\u043e\u0434 \u043a\u0430\u0442. <a name=\"habracut\"><\/a><\/p>\n<h4>\u041f\u0440\u0438\u043d\u044f\u0442\u0438\u0435<\/h4>\n<p>  \u041f\u0435\u0440\u0432\u044b\u043c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435\u043c \u0431\u044b\u043b\u043e \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0432 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442 \u0441 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u043c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u0412 \u043e\u0442\u0432\u0435\u0442 \u043d\u0430\u0448\u043b\u043e\u0441\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0439. \u0412 \u0441\u0430\u043c\u044b\u0445 \u0430\u0434\u0435\u043a\u0432\u0430\u0442\u043d\u044b\u0445 \u0432\u0441\u0435 \u0441\u0445\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u043a \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044e \u043c\u0435\u0442\u043e\u0434\u0430 fillBackgroundRectArray. \u0412 \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u0436\u0438\u0437\u043d\u0438 \u043e\u043d \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u043f\u043e\u043a\u0440\u0430\u0441\u043a\u0443 \u0442\u0435\u043a\u0441\u0442\u0430 \u043f\u0440\u0438 \u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 attributedText, \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u043e\u0433\u043e \u0432\u044b\u0448\u0435. \u0427\u0442\u043e\u0431\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e \u0437\u0430 \u0437\u0432\u0435\u0440\u044c, \u044f \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b \u0433\u043e\u0442\u043e\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0432 \u043d\u0430\u0434\u0435\u0436\u0434\u0435, \u0447\u0442\u043e \u0437\u0430\u0434\u0430\u0447\u0430 \u0432\u0441\u0435-\u0442\u0430\u043a\u0438 \u043d\u0430 \u043f\u0430\u0440\u0443 \u0447\u0430\u0441\u043e\u0432. \u041d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c. \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0442\u0432\u043e\u0440\u0438\u043b\u043e \u0447\u0442\u043e \u043f\u043e\u043f\u0430\u043b\u043e.<\/p>\n<h4>\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u2014 \u043d\u0430\u0448\u0435 \u0432\u0441\u0435<\/h4>\n<p>  \u0420\u0435\u0448\u0438\u0432 \u0431\u043e\u043b\u044c\u0448\u0435 \u0442\u0430\u043a \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c, \u044f \u043e\u0431\u0440\u0430\u0442\u0438\u043b\u0441\u044f \u043a \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438. \u041e\u0442\u0441\u044e\u0434\u0430 \u0441\u0442\u0430\u043b\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e UITextView \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a UIScrollView \u0434\u043b\u044f \u0442\u0435\u043a\u0441\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0440\u0443\u043b\u044f\u0442 \u0442\u0440\u0438 \u0442\u043e\u0432\u0430\u0440\u0438\u0449\u0430:<\/p>\n<ul>\n<li>NSTextStorage \u2014 \u043f\u043e \u0441\u0443\u0442\u0438 \u043e\u0431\u0435\u0440\u0442\u043a\u0430 \u043d\u0430\u0434 NSMutableAtributedString, \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u0430 \u0438 \u0435\u0433\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432; <\/li>\n<li>NSTextContainer \u2014 NSObject, \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0438\u0439 \u0437\u0430 \u0433\u0435\u043e\u043c\u0435\u0442\u0440\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0444\u0438\u0433\u0443\u0440\u0443, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u0442\u0435\u043a\u0441\u0442. \u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u044d\u0442\u043e \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a, \u0437\u0430\u043a\u0430\u0441\u0442\u043e\u043c\u0438\u0442\u044c \u043c\u043e\u0436\u043d\u043e \u0447\u0442\u043e \u0443\u0433\u043e\u0434\u043d\u043e;<\/li>\n<li>NSLayoutManager \u2014 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u0435\u0440\u0432\u044b\u043c\u0438 \u0434\u0432\u0443\u043c\u044f: \u0431\u0435\u0440\u0435\u0442 \u0442\u0435\u043a\u0441\u0442, \u0441\u0442\u0430\u0432\u0438\u0442 \u043e\u0442\u0441\u0442\u0443\u043f\u044b, \u0434\u0435\u043b\u0438\u0442 \u043d\u0430 \u0430\u0431\u0437\u0430\u0446\u044b \u0438 \u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u043d\u0430\u043c \u0437\u0430\u043a\u0440\u0430\u0441\u043a\u0443.<\/li>\n<\/ul>\n<p>  <\/p>\n<h4>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u2014 \u044d\u0442\u043e \u043a\u0440\u0443\u0442\u043e<\/h4>\n<p>  \u0412 \u0438\u0442\u043e\u0433\u0435 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043a \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0433\u043e NSLayoutManadger\u2019a \u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044e \u0432 \u043d\u0435\u043c \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430. <\/p>\n<pre><code class=\"swift\">class SelectionLayoutManager: NSLayoutManager {      override func fillBackgroundRectArray(_ rectArray: UnsafePointer&lt;CGRect&gt;, count rectCount: Int, forCharacterRange charRange: NSRange, color: UIColor) { \t }<\/code><\/pre>\n<p>  \u0412 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 fillBackgroundRectArray \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0438 \u0441\u043b\u043e\u0432 \u0438 \u0437\u0430\u043a\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442 \u0438\u0445. \u0422\u0430\u043a \u043a\u0430\u043a \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0435 \u0444\u0438\u0433\u0443\u0440\u044b, \u043a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u043e, \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0447\u0430\u0441\u0442\u044f\u043c\u0438 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u0441\u043e \u0441\u0442\u044b\u043a\u043e\u043c \u0433\u0434\u0435 \u043f\u043e\u043f\u0430\u043b\u043e, \u043e\u0442 \u043d\u0438\u0445 \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u043e\u0442\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f. \u041e\u0442\u0441\u044e\u0434\u0430 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 \u0440\u0430\u0437: \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0435 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043b\u0438\u043d\u0438\u0438 \u0438 \u043d\u0435\u0440\u0430\u0437\u0440\u044b\u0432\u043d\u044b \u0434\u043e \u0435\u0435 \u043a\u043e\u043d\u0446\u0430.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434, \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u044e\u0449\u0438\u0439 \u044d\u0442\u0443 \u0437\u0430\u0434\u0430\u0447\u0443, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c: \u043e\u043d \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u0432 \u0446\u0438\u043a\u043b\u0435 \u043f\u043e \u0441\u043b\u043e\u0432\u0430\u043c \u0430\u0431\u0437\u0430\u0446\u0430 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u0432\u043c\u0435\u0441\u0442\u0438\u0442\u0441\u044f \u043b\u0438 \u0432 \u043e\u0434\u043d\u0443 \u043b\u0438\u043d\u0438\u044e \u0441\u0442\u0440\u043e\u043a\u0430, \u0435\u0441\u043b\u0438 \u043a \u043d\u0435\u0439 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0441\u043b\u043e\u0432\u043e? \u0415\u0441\u043b\u0438 \u043d\u0435\u0442, \u0442\u043e \u044d\u0442\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0430\u044f \u043f\u043e\u043b\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430, \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e. \u0415\u0441\u043b\u0438 \u0434\u0430 \u2014 \u0431\u0435\u0440\u0451\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0441\u043b\u043e\u0432\u043e \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u0441\u043d\u043e\u0432\u0430. \u0414\u0430, \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u0440\u0430\u0439\u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u0430, \u0431\u044b\u043b\u0430 \u0437\u0430\u0434\u0443\u043c\u043a\u0430 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434, \u043d\u043e \u0440\u0443\u043a\u0438 \u043f\u043e\u043a\u0430 \u043d\u0435 \u0434\u043e\u0448\u043b\u0438. \u0412 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u043f\u0440\u043e\u0448\u0443 \u0432\u0430\u0448\u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b. <\/p>\n<pre><code class=\"swift\">private func detectLines(from string: String, width: CGFloat, font: UIFont) -&gt; [String] {     var strings: [String] = []     var cumulativeString = \"\"     let words = string.components(separatedBy: CharacterSet.whitespaces)          for word in words {       let checkingString = cumulativeString + word       if checkingString.size(withFont: font).width &lt; width {         cumulativeString.append(word)       } else {         if cumulativeString.isNotEmpty {           strings.append(cumulativeString)         }                  if word.size(withFont: font).width &lt; width {           cumulativeString = word         } else {           var stringsFromWord: [String] = []           var handlingWord = word                      while handlingWord.isNotEmpty {             let fullFillString = detectFullFillString(from: handlingWord, width: width, font: font)             stringsFromWord.append(fullFillString)             handlingWord = word.replacingOccurrences(of: stringsFromWord.reduce(\"\") { $0 + $1 }, with: \"\")           }                      stringsFromWord.removeLast()           strings.append(contentsOf: stringsFromWord)           let remainString = word.replacingOccurrences(of: stringsFromWord.reduce(\"\") { $0 + $1 }, with: \"\")           cumulativeString = remainString         }       }            }          if cumulativeString.isNotEmpty {       strings.append(cumulativeString)     }          return strings   }<\/code><\/pre>\n<p>  \u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043e\u0441\u043e\u0431\u044b\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u0441\u043b\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0430\u043c\u0438 \u043f\u043e \u0441\u0435\u0431\u0435 \u0432 \u043e\u0434\u043d\u0443 \u043b\u0438\u043d\u0438\u044e \u043d\u0435 \u043f\u043e\u043c\u0435\u0449\u0430\u044e\u0442\u0441\u044f. UITextView \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u043d\u0438\u043c\u0438 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e \u2014 \u043f\u0435\u0440\u0435\u043d\u043e\u0441 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435 \u0432\u043e\u0448\u043b\u0430. \u0414\u0443\u0431\u043b\u0438\u0440\u0443\u0435\u043c \u0434\u0430\u043d\u043d\u0443\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0435 \u0441 \u0442\u0435\u043c \u0436\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0445\u043e\u0434\u0430, \u043d\u043e \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430\u043c. \u0427\u0442\u043e \u0432\u043c\u0435\u0441\u0442\u0438\u043b\u043e\u0441\u044c: \u043f\u043e\u043b\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430, \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0435 \u2014 \u043b\u0438\u0431\u043e \u0442\u043e\u0436\u0435 \u043f\u043e\u043b\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430, \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0447\u0435\u043d\u044c \u0434\u043b\u0438\u043d\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0432\u0430, \u043b\u0438\u0431\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u043e\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e \u043d\u0430 \u043d\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435.<\/p>\n<pre><code class=\"swift\">private func detectFullFillString(from word: String, width: CGFloat, font: UIFont) -&gt; String {     var string = \"\"          for character in word {       let checkingString = string.appending(String(character))       if checkingString.size(withFont: font).width &gt; width {         break       } else {         string.append(contentsOf: String(character))       }     }          return string   }<\/code><\/pre>\n<p>  \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u043c\u0435\u0442\u043e\u0434\u0430 detectLines(from:width:font:) \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u0442\u0440\u043e\u043a, \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043f\u043e\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0445 \u043f\u043e \u043b\u0438\u043d\u0438\u044f\u043c. \u0414\u0430\u043b\u0435\u0435 \u0438\u0437 \u043c\u0435\u0442\u043e\u0434\u0430 frames(for lines:width:font) \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442 \u0438 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u0432 \u043b\u0438\u043d\u0438\u0439. <\/p>\n<pre><code class=\"swift\">private func frames(for lines: [String], width: CGFloat, font: UIFont) -&gt; [CGRect] {     var rects: [CGRect] = []     let stringsSizes = lines.map { $0.size(withFont: font) }     stringsSizes.forEach {       let rect = CGRect(origin: CGPoint(x: (width - $0.width) \/ 2,                                         y: $0.height * CGFloat(rects.count)),                         size: $0)       rects.append(rect)     }          return rects   }<\/code><\/pre>\n<p>  <\/p>\n<h4>\u041d\u0430\u0432\u043e\u0434\u0438\u043c \u043a\u0440\u0430\u0441\u043e\u0442\u0443<\/h4>\n<p>  \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 \u043d\u043e\u043c\u0435\u0440 \u0434\u0432\u0430: \u0437\u0430\u043a\u0440\u0430\u0441\u0438\u0442\u044c \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0438. \u041f\u043e\u0434 \u043f\u043e\u043d\u044f\u0442\u0438\u0435\u043c \u00ab\u043a\u0440\u0430\u0441\u0438\u0432\u043e\u00bb \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u043b\u043e\u0441\u044c \u0437\u0430\u043a\u0440\u0430\u0448\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u043c \u0446\u0432\u0435\u0442\u043e\u043c \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0430 \u0441\u043e \u0441\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435\u043c \u0443\u0433\u043b\u043e\u0432. \u0420\u0435\u0448\u0435\u043d\u0438\u0435: \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0430 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u043f\u043e \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u0430\u043c \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c UIBezierPath. \u0427\u0442\u043e\u0431\u044b \u0441\u0442\u044b\u043a\u0438 \u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438\u0441\u044c \u043b\u0443\u0447\u0448\u0435, \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043a\u0440\u0443\u0433\u043b\u044f\u0442\u044c \u043a\u0440\u0430\u044f \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u0435\u043d\u044c\u0448\u0435 \u043f\u043e \u0448\u0438\u0440\u0438\u043d\u0435. \u041c\u0435\u0442\u043e\u0434 \u043f\u0440\u043e\u0441\u0442: \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u043c \u043f\u043e \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u0430\u043c \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0430 \u0438 \u0440\u0438\u0441\u0443\u0435\u043c \u043a\u043e\u043d\u0442\u0443\u0440.<\/p>\n<pre><code class=\"swift\">private func path(from rects: [CGRect], cornerRadius: CGFloat, horizontalInset: CGFloat) -&gt; CGPath {     let path = CGMutablePath()          rects.enumerated().forEach { (index, rect) in              let hasPrevious = index &gt; 0       let isPreviousWider = hasPrevious ? rects[index - 1].width &gt;= rect.width || abs(rects[index - 1].width - rect.width) &lt; 5 : false              let hasNext = index != rects.count - 1       let isNextWider = hasNext ? rects[index + 1].width &gt;= rect.width || abs(rects[index + 1].width - rect.width) &lt; 5 : false              path.move(to: CGPoint(x: rect.minX - horizontalInset + (isPreviousWider ? 0 : cornerRadius),                             y: rect.minY))       \/\/      top       path.addLine(to: CGPoint(x: rect.maxX + horizontalInset - (isPreviousWider ? 0 : cornerRadius),                                y: rect.minY))              if isPreviousWider == false {         path.addQuadCurve(to: CGPoint(x: rect.maxX + horizontalInset,                                       y: rect.minY + cornerRadius),                           control: CGPoint(x: rect.maxX + horizontalInset,                                            y: rect.minY))       }              \/\/      right       path.addLine(to: CGPoint(x: rect.maxX + horizontalInset,                                y: rect.maxY - (isNextWider ? 0 : cornerRadius)))              if isNextWider == false {         path.addQuadCurve(to: CGPoint(x: rect.maxX + horizontalInset - cornerRadius,                                       y: rect.maxY),                           control: CGPoint(x: rect.maxX + horizontalInset,                                            y: rect.maxY))       }              \/\/      bottom       path.addLine(to: CGPoint(x: rect.minX - horizontalInset + (isNextWider ? 0 : cornerRadius),                                y: rect.maxY))              if isNextWider == false {         path.addQuadCurve(to: CGPoint(x: rect.minX - horizontalInset,                                       y: rect.maxY - cornerRadius),                           control: CGPoint(x: rect.minX - horizontalInset,                                            y: rect.maxY))       }              \/\/      left       path.addLine(to: CGPoint(x: rect.minX - horizontalInset,                                y: rect.minY + (isPreviousWider ? 0 : cornerRadius)))              if isPreviousWider == false {         path.addQuadCurve(to: CGPoint(x: rect.minX - horizontalInset + cornerRadius,                                       y: rect.minY),                           control: CGPoint(x: rect.minX - horizontalInset,                                            y: rect.minY))       }              path.closeSubpath()            }          return path   }<\/code><\/pre>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 draw(_:color:) \u0437\u0430\u043b\u0438\u0432\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0443\u0440.<\/p>\n<pre><code class=\"swift\">private func draw(_ path: CGPath, color: UIColor) {     color.set()     if let ctx = UIGraphicsGetCurrentContext() {       ctx.setAllowsAntialiasing(true)       ctx.setShouldAntialias(true)              ctx.addPath(path)       ctx.drawPath(using: .fillStroke)     }   }<\/code><\/pre>\n<p>  \u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u043c\u0435\u0442\u043e\u0434\u0430 fillBackgroundRectArray(_:count:forCharacterRange:color:)<\/p>\n<pre><code class=\"swift\">override func fillBackgroundRectArray(_ rectArray: UnsafePointer&lt;CGRect&gt;, count rectCount: Int, forCharacterRange charRange: NSRange, color: UIColor) {     let cornerRadius: CGFloat = 8     let horizontalInset: CGFloat = 5          guard       let font = (textStorage?.attributes(at: 0, effectiveRange: nil).first { $0.key == .font })?.value as? UIFont,       let textContainerWidth = textContainers.first?.size.width       else { return }          \/\/\/ Divide the text into paragraphs     let lines = paragraphs(from: textStorage?.string ?? \"\")          \/\/\/ Divide the paragraphs into separate lines     let strings = detectLines(from: lines, width: textContainerWidth, font: font)          \/\/\/ Get rects from the lines     let rects = frames(for: strings, width: textContainerWidth, font: font)          \/\/\/ Get a contour by rects     let rectsPath = path(from: rects, cornerRadius: cornerRadius, horizontalInset: horizontalInset)          \/\/\/ Fill it     draw(rectsPath, color: color)   }<\/code><\/pre>\n<p>  \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c. \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c. \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430 \u043e\u0442\u043b\u0438\u0447\u043d\u043e.<\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/zi\/k3\/2p\/zik32p4lq1wi3u8hg-my0qzfqfi.gif\"><\/div>\n<p>  \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f: \u043a\u0430\u0441\u0442\u043e\u043c \u2014 \u044d\u0442\u043e \u0438\u043d\u043e\u0433\u0434\u0430 \u0441\u043b\u043e\u0436\u043d\u043e \u0438 \u0442\u043e\u0448\u043d\u043e, \u043d\u043e \u043a\u0430\u043a \u0436\u0435 \u043a\u0440\u0430\u0441\u0438\u0432\u043e, \u043a\u043e\u0433\u0434\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u0430\u043a \u043d\u0443\u0436\u043d\u043e. \u0422\u0432\u043e\u0440\u0438\u0442\u0435 \u043a\u0430\u0441\u0442\u043e\u043c, \u044d\u0442\u043e \u0437\u0434\u043e\u0440\u043e\u0432\u043e.<\/p>\n<p>  \u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0442\u0443\u0442 <a href=\"https:\/\/github.com\/MikhailZimin\/SelectionLayoutManager\/blob\/master\/SelectionLayoutManager\">SelectionLayoutManager<\/a>.<\/p>\n<h4>\u0421\u0441\u044b\u043b\u043a\u0438<\/h4>\n<p>  <\/p>\n<ol>\n<li><a href=\"https:\/\/developer.apple.com\/documentation\/uikit\/nslayoutmanager\">NSLayoutManager<\/a><\/li>\n<li><a href=\"https:\/\/developer.apple.com\/documentation\/uikit\/nstextcontainer\">NSTextContainer<\/a><\/li>\n<li><a href=\"https:\/\/developer.apple.com\/documentation\/appkit\/nstextview\">NSTextView<\/a><\/li>\n<li><a href=\"https:\/\/developer.apple.com\/library\/archive\/documentation\/StringsTextFonts\/Conceptual\/TextAndWebiPhoneOS\/CustomTextProcessing\/CustomTextProcessing.html#\/\/apple_ref\/doc\/uid\/TP40009542-CH4-SW1\">Using Text Kit to Draw and Manage Text<\/a><\/li>\n<\/ol>\n<\/div>\n<p>               <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d() } } }t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>      <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/479992\/\"> https:\/\/habr.com\/ru\/post\/479992\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/479992\/\">\n<h4>\u041f\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0438<\/h4>\n<p>  \u0414\u0430\u043d\u043e: \u043c\u043d\u043e\u0433\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442. <br \/>  \u041d\u0430\u0439\u0442\u0438: \u043a\u0440\u0430\u0441\u0438\u0432\u043e \u043e\u0444\u043e\u0440\u043c\u043b\u0435\u043d\u043d\u044b\u0439 \u0444\u043e\u043d.<\/p>\n<p>  \u00ab\u0414\u0430 \u044d\u0442\u043e \u0436\u0435 \u043d\u0430 \u0447\u0430\u0441\u043e\u043a\u00bb, \u2014 \u043f\u043e\u0434\u0443\u043c\u0430\u043b \u044f. \u2014 \u041d\u0443\u0436\u043d\u043e \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c backgroundColor \u0432 attributedText\u00bb. \u041d\u043e \u044d\u0442\u043e\u0433\u043e \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e. \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0435 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u2014 \u044d\u0442\u043e \u0437\u0430\u043a\u0440\u0430\u0448\u0435\u043d\u043d\u044b\u0439 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a. \u041d\u0435\u043a\u0440\u0430\u0441\u0438\u0432\u043e. \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u2014 \u043d\u0443\u0436\u0435\u043d \u043a\u0430\u0441\u0442\u043e\u043c! \u0422\u0435\u0440\u043d\u0438\u0441\u0442\u044b\u0439 \u043f\u0443\u0442\u044c \u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u0430\u0442\u043e\u043b\u043a\u043d\u0443\u043b \u043c\u0435\u043d\u044f \u043d\u0430 \u043c\u044b\u0441\u043b\u044c \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u0434\u0430\u0431\u044b \u0431\u0443\u0434\u0443\u0449\u0438\u043c \u043f\u043e\u043a\u043e\u043b\u0435\u043d\u0438\u044f\u043c \u043d\u0435 \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u0430\u0434\u0430\u0442\u044c. \u0417\u0430\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043e\u0432\u0430\u0432\u0448\u0438\u0445\u0441\u044f \u043f\u0440\u043e\u0448\u0443 \u043f\u043e\u0434 \u043a\u0430\u0442. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-295697","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/295697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=295697"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/295697\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=295697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=295697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=295697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}