When we use Uitextview as a uilabel for some needs (in order to use Uitextview's own copy, paste, select function, automatic phone identification, email, link, etc.), we only need to disable the Uitextview of a few properties on the line
Textview.editable = no;//Not editable
textview.scrollenabled = no;//Not scrollable
Then calculate the size of the text and set the Uitextview frame, which I am here is the xib inside the TextView,
nsmutableattributedstring *attributedstring = [[nsmutableattributedstring alloc]initwithstring: Notification.content];
Nsmutableparagraphstyle *paragraphstyle = [[Nsmutableparagraphstyle alloc]init];
[Paragraphstyle Setlinespacing:5];
Nsrange range = Nsmakerange (0,notification.content.length);
[Attributedstring addattribute:nsparagraphstyleattributename Value:paragraphstyle Range:range];
[Attributedstring addattribute:nsforegroundcolorattributename Value:[uicolor colorwithhex:0x333333] range:range];
[Attributedstring addattribute:nsfontattributename Value:[uifont systemfontofsize:15.0] range:range];
Self.textView.attributedText = nil;
Self.textView.attributedText = attributedstring;
Cgsize size = [Self.textView.attributedText boundingrectwithsize:cgsizemake (screenwidth-2*16, maxfloat) options: Nsstringdrawinguseslinefragmentorigin context:nil].size;
Self.textViewH.constant = Size.Height;
There is no problem with using this method on the Uilabel, but the text display is incomplete, or some of the calculated height is larger than the actual height of the text, so you need to set some properties:
[Self.textview Setcontentinset:uiedgeinsetsmake (-10,-5,-15,-5)];//sets Uitextview's inner margin [self.textview
settextalignment:nstextalignmentleft];//and set left-aligned self.textView.layoutManager.allowsNonContiguousLayout = NO; self.textView.scrollEnabled = YES; If Scrollenabled=no, the computed is not correct, although this is set to Yes, but TextView does not actually scroll and display the content correctly