NSTextAttachment with text flowing around it - objective-c

I have a TextKit based editor with support to adding images. I want to show each image on separate lines.
My code looks like this (Thank you TextEdit), in my subclass of NSTextStorage
- (void)addImageAssets:(NSArray *)allAssets atRange:(NSRange)range;
{
NSMutableAttributedString *attachments = [NSMutableAttributedString new];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentCenter;
paragraphStyle.lineSpacing = 20.0f;
for (ALAsset *newAsset in allAssets)
{
UIImage *theImage = [UIImage imageWithCGImage:newAsset.aspectRatioThumbnail];
NSTextAttachment *textAttachment = [NSTextAttachment new];
textAttachment.image = theImage;
NSMutableAttributedString *replacementString = [NSMutableAttributedString new];
[replacementString appendAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment]];
[replacementString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, [replacementString length])];
[attachments appendAttributedString:replacementString];
}
_isEditing = YES;
[self beginEditing];
[_backingStore replaceCharactersInRange:range
withAttributedString:attachments];
[self edited:NSTextStorageEditedAttributes
range:range changeInLength:allAssets.count];
[super processEditing];
[self endEditing];
_isEditing = NO;
}
(the _isEditing is a boolean flag used for book-keeping)
The output looks like this Output http://take.ms/QCvRK
I have tried various parameters on the NSMutableParagraphStyle but I couldn't get a line break after each image.
Appending a line break ("\r") around the text attachment will result in a glyph error
!!! _NSLayoutTreeLineFragmentRectForGlyphAtIndex invalid glyph index 1
!!! _NSGlyphTreeInvalidateGlyphsForCharacterRange invalid char range 1
!!! _NSGlyphTreeInvalidateGlyphsForCharacterRange character count mismatch
!!! _NSLayoutTreeLineFragmentRectForGlyphAtIndex invalid glyph index 0
!!! _NSLayoutTreeLineFragmentRectForGlyphAtIndex invalid glyph index 0
!!! _NSLayoutTreeLineFragmentUsedRectForGlyphAtIndex invalid glyph index 2147483647
I tried to subclass the NSTextAttachment to over-ride the attachmentBoundsForTextContainer:proposedLineFragment:glyphPosition:characterIndex: so that the width is set to the device width, this resulted in the attached image looking blown up.
Any suggestion on how I can introduce a line break right after each image ? Also it would be great if I can get the text to flow around the image attachments.
Thanks in advance.

Related

how to Change message body text bold in message composer

hi I need to change text to bold in message composer body pls help me , I've tried with attributed text but I couldn't add
NSMutableAttributedString *yourAttributedString = [[NSMutableAttributedString alloc] initWithString:message];
NSString *boldString = #"hey";
NSRange boldRange = [message rangeOfString:boldString];
[yourAttributedString addAttribute: NSFontAttributeName value:[UIFont boldSystemFontOfSize:18] range:boldRange];
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
lbl.attributedText = yourAttributedString;
MFMessageComposeViewController *messageComposeVC = [[MFMessageComposeViewController alloc] init];
messageComposeVC.messageComposeDelegate = self;
messageComposeVC.recipients = selectedContacts;
messageComposeVC.body = [lbl.attributedText string];
[self presentViewController:messageComposeVC animated:YES completion:nil];
I think it is better if you initiate a attribute to hold all your text attributes ( text size, font , colors ,weight ,styles) and add that when you are initializing the text , in your case "yourAttributedString" . Not sure if there is a specific reason to as why you do this so but try the following .
Use LocalizedStandardRangeofString instead or RangeOfString as you are
displaying this text.Check the "important" section in this documentation https://developer.apple.com/documentation/foundation/nsstring/1416849-rangeofstring?language=objc
//make an attributes dictionary here.
NSDictionary *stringAttributes = #{
NSFontAttributeName:[UIFont boldSystemFontOfSize:20]
};
NSString *message = #" message hey";
NSMutableAttributedString *yourAttributedString =[[NSMutableAttributedString alloc]initWithString:message];
NSString *boldString = #"hey";
NSRange range = [message localizedStandardRangeOfString:boldString];
[yourAttributedString addAttributes:stringAttributes range:range];
self.lbl.attributedText = yourAttributedString;

NSImage lockFocus and NSString size on retina display

I'm facing a weird issue, I'm drawing inside an NSImage using the following pseudo-code:
NSString* text = #"Hello world!";
NSDictionary *dict = [[[NSDictionary alloc] initWithObjectsAndKeys:[NSColor colorWithCGColor:textColor],NSForegroundColorAttributeName,font, NSFontAttributeName,nil] autorelease];
NSMutableAttributedString* str = [[[NSMutableAttributedString alloc] initWithString:text attributes:dict] autorelease];
NSSize stringSize = [str size];
NSImage* image = [[[NSImage alloc] initWithSize:stringSize] autorelease];
[image lockFocus];
NSRect drawRect = NSMakeRect(0,0,stringSize.width,stringSize.height);
[str drawInRect:drawRect];
[image unlockFocus];
Now the problem is that, with a dual monitor configuration, if I keep my retina display open, the string is mangled (I get half of the string drawn), while by simply closing my retina display and using only my cinema display, the string is drawn correctly. It's like the NSImage is getting the default context and some scaling factor from the retina display.
Do you have any hints ?
Thanks !
Ok, I will keep this for future reference, even there's something about displaying NSImage that covers the same aspect.
No matter what's your primary display but seems that the NSGraphicContext comes with an affine transformation that multiplies x 2 to address the retina resolution.
You just need to reset the affine transformations, before drawing into NSImage with:
NSAffineTransform *trans = [[[NSAffineTransform alloc] init] autorelease];
[trans set];

How do I center a number in a circle?

I'm trying to produce a set of custom page numbers. The current page number will be displayed. The others will simply be open circles.
Using the code below, I'm getting the desired results - except, with the new text attributes in iOS7, I cannot figure out how to center the number.
Any help will be appreciated.
Original iOS6 code:
[pageNumber drawInRect:CGRectMake(x,(self.frame.size.height-_currentPageDiameter)/2-1,_currentPageDiameter,_currentPageDiameter) withFont:[UIFont systemFontOfSize:_currentPageDiameter-2] lineBreakMode:UILineBreakModeCharacterWrap alignment:NSTextAlignmentCenter];
My iOS7 code:
NSString *pageNumber = [NSString stringWithFormat:#"%i", i+1];
CGContextSetFillColorWithColor(myContext, [[UIColor whiteColor] CGColor]);
UIFont* font = [UIFont systemFontOfSize:_currentPageDiameter-2];
NSDictionary *attrs = #{ NSForegroundColorAttributeName : [UIColor whiteColor],
NSFontAttributeName : font,
NSTextEffectAttributeName : NSTextEffectLetterpressStyle};
CGRect r = CGRectMake(x,(self.frame.size.height-_currentPageDiameter)/2-1,_currentPageDiameter,_currentPageDiameter);
[pageNumber drawInRect:r withAttributes:attrs];
The property you're missing for center alignment is defined the following way. I also added the line break mode you had defined on the iOS6 code:
NSMutableParagraphStyle *paragrapStyle = [[NSMutableParagraphStyle alloc] init];
paragrapStyle.alignment = NSTextAlignmentCenter;
paragrapStyle.lineBreakMode = NSLineBreakByCharWrapping;
You just place them in the attributes dictionary under the key:
NSArray *attrs = #{
NSParagraphStyleAttributeName : paragraphStyle,
...
};

How do I give NSText multiple shadows?

Right now I'm using
NSShadow *textShadow = [NSShadow new];
textShadow.shadowBlurRadius = 5;
textShadow.shadowColor = [[NSColor whiteColor] colorWithAlphaComponent:.5];
[self addAttribute:NSShadowAttributeName value:textShadow range:NSMakeRange(0, self.length)];
from an NSTextStorage to give text a shadow. But I want to apply more than one shadow, and adding another NSShadowAttributeName just overwrites the previous value.
How can I add more than one shadow? Can it be done with CGContextSetShadowWithColor?
Not sure please try this below code for your textview. When you write string inside textview it will select that much range and on the basis of that it draws the color:-
-(IBAction)createNewTabView:(id)sender
{
NSString *allTheText =[tv string];
NSArray *lines = [allTheText componentsSeparatedByString:#"\n"];
NSString *str=[[NSString alloc]init];
NSMutableAttributedString *attr;
BOOL isNext=YES;
[tv setString:#""];
for (str in lines)
{
attr=[[NSMutableAttributedString alloc]initWithString:str];
if ([str length] > 0)
{
NSRange range=NSMakeRange(0, [str length]);
[attr addAttribute:NSBackgroundColorAttributeName value:[NSColor greenColor] range:range];
[tv .textStorage appendAttributedString:attr];
isNext=YES;
}
else
{
NSString *str=#"\n";
NSAttributedString *attr=[[NSAttributedString alloc]initWithString:str];
[tv .textStorage appendAttributedString:attr];
isNext=NO;
}
if (isNext==YES)
{
NSString *str=#"\n";
NSAttributedString *attr=[[NSAttributedString alloc]initWithString:str];
[tv .textStorage appendAttributedString:attr];
}
}
}
I would suggest the "caveman" approach. Instead of trying to get duplicate shadows, use duplicate Text's, all but one of which has it's color set to the clear color, and give them different dropShadows.
make two ore more text's that line up exactly.
make the front most your "real" text color.
make the other text's clear color.
set one type of shadow for each type of text.
You should be able to make a class that automates this, if you are going to use it multiple times.
This is a sample of what I got:
oh-- if you don't make the additional text's your text will appear much darker/bolder than you expect.

How to change only font size for the whole styled text in NSTextView

I need to set a text size (for example to 42) of the selected rich text which uses multiple fonts.
I imagine I can check attributes of each group of characters, modify the font size and set attributes back, but looking at the floating Font panel it seems like there should be a very easy and straightforward way to accomplish that. Do I miss something obvious?
On 10.6 there is a convenient way to iterate over the attributes and increase the font size.
This method can be added to an NSTextView category.
- (IBAction)increaseFontSize:(id)sender
{
NSTextStorage *textStorage = [self textStorage];
[textStorage beginEditing];
[textStorage enumerateAttributesInRange: NSMakeRange(0, [textStorage length])
options: 0
usingBlock: ^(NSDictionary *attributesDictionary,
NSRange range,
BOOL *stop)
{
#pragma unused(stop)
NSFont *font = [attributesDictionary objectForKey:NSFontAttributeName];
if (font) {
[textStorage removeAttribute:NSFontAttributeName range:range];
font = [[NSFontManager sharedFontManager] convertFont:font toSize:[font pointSize] + 1];
[textStorage addAttribute:NSFontAttributeName value:font range:range];
}
}];
[textStorage endEditing];
[self didChangeText];
}
Generalizing on Jonathan's answer a bit, here is a category interface you can simply paste into appropriate files in your Xcode project:
#interface NSTextView (FrameworkAdditions)
- (IBAction)decrementFontSize:(id)sender;
- (IBAction)incrementFontSize:(id)sender;
#end
And the corresponding implementation:
#implementation NSTextView (FrameworkAdditions)
- (void)changeFontSize:(CGFloat)delta;
{
NSFontManager * fontManager = [NSFontManager sharedFontManager];
NSTextStorage * textStorage = [self textStorage];
[textStorage beginEditing];
[textStorage enumerateAttribute:NSFontAttributeName
inRange:NSMakeRange(0, [textStorage length])
options:0
usingBlock:^(id value,
NSRange range,
BOOL * stop)
{
NSFont * font = value;
font = [fontManager convertFont:font
toSize:[font pointSize] + delta];
if (font != nil) {
[textStorage removeAttribute:NSFontAttributeName
range:range];
[textStorage addAttribute:NSFontAttributeName
value:font
range:range];
}
}];
[textStorage endEditing];
[self didChangeText];
}
- (IBAction)decrementFontSize:(id)sender;
{
[self changeFontSize:-1.0];
}
- (IBAction)incrementFontSize:(id)sender;
{
[self changeFontSize:1.0];
}
#end
This will double the font size, but you may change the scale property to any value, or provide your fixed size
NSFont * font = ...;
CGFloat fontSize = [[font fontDescriptor].fontAttributes[NSFontSizeAttribute] floatValue];
font = [NSFont fontWithDescriptor:[font fontDescriptor] size:fontSize * 2.];
self.textField.font = font;
Note: I assume that you are using a NSTextView and that you can access its text storage (NSTextStorage).
I think it is not possible to only change the font's size over a text that use multiple fonts. In NSAttributedString, font's size is part of the NSFontAttributeName attribute which controls both the font and the size.
One solution is to iterate over the selection and use the attribute:atIndex:longestEffectiveRange:inRange: to capture the range when each font apply, change the font's size and then use the addAttribute:value:range: to set the new font over the range.
Update:
If you take a look at the GNUstep GUI source code for NSTextView (under LGPL), you will see that their implementation use the range iteration.
Since NSTextView is a subclass of NSView, you can use -scaleUnitSquareToSize: to change the magnification level of the text view. For example, to make all the text double sized you'd call:
[textView scaleUnitSquareToSize:NSMakeSize(2.0, 2.0)];
You may need to make some adjustments to the dimensions of the text view's NSTextContainer after performing this operation to ensure the text is laid out correctly.