UITextView changes font when detects action

469 Views Asked by At

I've been looking for a solution to this problem for a while, and no one seems to have come across a similar issue.

Basically I have multiple UITextViews that I use to detect addresses, urls, phone numbers, etc (anything that can be detected via UIDataDectorTypeAll) from some EKEvent.notes. I then add these UITextViews as subviews of a UIScrollView.

Now, for some reason or another, once the UITextView detects an address or a phone number and becomes an actionable target, it will randomly draw with a font 2x its specified font!

I've setup tests to just redraw my views if I tap. When the UITextView is added to the view initially, I can see in black the proper text. Then it does its detection deal and becomes a actionable target. Sometimes it stays the proper size, sometimes it draws at 2x font (but still in proper frame, thus it gets clipped).

It's very straight forward, but here's my code below. All variable are correct values, frame is correct, text is correct, everything is correct and about 50% of the time it draws correct. Its just that other 50% of the time it becomes (apparently) 2x font! Any help is greatly appreciated!

UITextView *locationTextView = [[UITextView alloc] init];
locationTextView.dataDetectorTypes = UIDataDetectorTypeAll;
locationTextView.text = location;
locationTextView.font = [UIFont fontWithName:@"AvenirNext-Regular" size:17];
locationTextView.editable = NO;
locationTextView.userInteractionEnabled = YES;
locationTextView.contentInset = UIEdgeInsetsMake(-8,-8,-8,-8);
locationTextView.frame =CGRectMake(kBufferLeft, daySize.height, kBufferDayViewTextWidth, locationSize.height);
[scrollView addSubview:locationTextView];

Correct: https://i.stack.imgur.com/1TdYP.jpg

Incorrect: https://i.stack.imgur.com/s7SMQ.jpg

(Not allowed to post images yet, sorry.)

Same exact code produced both effect. Thank you for your time.

Cheers!

EDIT: I went with TTTAttributedLabels to fix this issue.

github.com/mattt/TTTAttributedLabel

2

There are 2 best solutions below

0
Prabhat Pankaj On

You can set font at <UITextField> delegate.

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 

{ 
  locationTextView.font = [UIFont fontWithName:@"AvenirNext-Regular" size:17];
}
0
Nuthinking On

I had the same problem because I was using a custom line breaking (layoutManager:shouldBreakLineByWordBeforeCharacterAtIndex:). Had to disable that.