How to control the string bold with coreText??? - ipad

CGFloat widthValue = -1.0;
CFNumberRef strokeWidth = CFNumberCreate(NULL,kCFNumberFloatType,&widthValue);
//Setup the attributes dictionary with font and color
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
(id)font, (id)kCTFontAttributeName,
_fontColor.CGColor, kCTForegroundColorAttributeName,
strokeWidth,kCTStrokeWidthAttributeName,
nil];
I set the kCTStrokeWidthAttributeName with the above code ,but it is still bold.
I don't want the string to be bold.
How i can control it ????

Checkout kCTFontWeightTrait and kCTFontBoldTrait.

Try changing your widthValue to:
CGFloat widthValue = 0.f;
to remove the bold text.
From Apple's docs:
kCTStrokeWidthAttributeName
The stroke width. Value must be a CFNumberRef object. Default value is 0.0, or no stroke. This attribute, interpreted as a
percentage of font point size, controls the text drawing mode:
positive values effect drawing with stroke only; negative values are
for stroke and fill. A typical value for outlined text is 3.0.

Related

use coretext ,when draw images and in ios6 , i get CTFrame empty values

CTFrame: visible string range = (0, 0){type = mutable-small, count = 0, values = ()}
i find this problem only happens in ios6,and only drawing images.final,i find this code maybe the problem at :
if (imagename) {
//render empty space for drawing the image in the text //1
CTRunDelegateCallbacks callbacks;
callbacks.version = kCTRunDelegateVersion1;
callbacks.getAscent = ascentCallback;
callbacks.getDescent = descentCallback;
callbacks.getWidth = widthCallback;
callbacks.dealloc = deallocCallback;
NSDictionary *imgAttr = #{#"width": [NSNumber numberWithFloat:self.customFaceSize.width],
#"height": [NSNumber numberWithFloat:self.customFaceSize.height],
#"descent" : [NSNumber numberWithFloat:0],
};
CFRetain((__bridge CFTypeRef)imgAttr);
CTRunDelegateRef delegate = CTRunDelegateCreate(&callbacks, (__bridge void *)(imgAttr));
NSDictionary *attrDictionaryDelegate = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge id)delegate,(NSString *)kCTRunDelegateAttributeName,
imagename,iCKeyFaceName,
[NSNumber numberWithLong:styleRange.location],iCkeyFaceLocation,
nil];
[*attributedString addAttributes:attrDictionaryDelegate range:styleRange];
[*attributedString addAttributes:[self attributesWithNude:nude] range:styleRange];
}else{
[*attributedString addAttributes:[self attributesWithNude:nude] range:styleRange];
}
how can i do? thanks
I fix this issue,i found the rect for path is something difference in ios6.
CGRect drawingRect = self.bounds;
CGPathAddRect(path, NULL, drawingRect);
CTFrameRef textFrame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0), path, NULL);
drawingRect.size.height is less than which in ios7 and upper
so.i fix it by add little pixel for drawingRect.size.height in ios6,and it work.
hope,will help you.
drawingRect.size.height += 2;
i am chinese,and i solved this problem.maybe this is not u'r problem.
but,i think i should make a record. and , my english is just so so !!!
ok,final i found what's the problem.Here is the step:
1.i found "CGPathAddRect" method, different rect draws different
2.so,i think CTRunDelegateRef is not the problem
3.then ,i think i should check the attributedString first
4. i compared attributedString between working and not working
5. i found in the attributedString that not working,the image run,font is 17, but woking is 13
6. so i think the attributed text font is the problem
7. then, i checked attributed styles ,i found the faceStyle font is larger than default.
so,change this font to the default style font .and it works !

UITextView text is loosing font size when i set an Attribute on NSTextStorage with NSUnderlineStyleAttributeName

In my App i have a UItextView and its input view has accessory view with couple of buttons on it.
I am trying to change the text already being typed to underline text. Its working fine but its loosing the font and the font size after conversion.
Here's what i am doing:
NSRange selectedRange = [self.commentsTextView selectedRange];
NSDictionary *currentAttributesDict = [self.commentsTextView.textStorage attributesAtIndex:selectedRange.location
effectiveRange:nil];
NSDictionary *dict;
if ([currentAttributesDict objectForKey:NSUnderlineStyleAttributeName] == nil ||
[[currentAttributesDict objectForKey:NSUnderlineStyleAttributeName] intValue] == 0) {
dict = #{NSUnderlineStyleAttributeName: [NSNumber numberWithInt:1]};
}
else{
dict = #{NSUnderlineStyleAttributeName: [NSNumber numberWithInt:0]};
}
[self.commentsTextView.textStorage beginEditing];
[self.commentsTextView.textStorage setAttributes:dict range:selectedRange];
[self.commentsTextView.textStorage endEditing];
You're setting the attributes for that range of text using a dictionary that only has a value for NSUnderlineStyleAttributeName.
In your if... else structure, in addition to NSUnderlineStyleAttributeName give dict the values that you want for the font and font size.
How did you previously set the font and font size?
From Apple's doc on UITextView the Overview section:
In iOS 6 and later, this class supports multiple text styles through
use of the attributedText property. (Styled text is not supported in
earlier versions of iOS.) Setting a value for this property causes the
text view to use the style information provided in the attributed
string. You can still use the font, textColor, and textAlignment
properties to set style attributes, but those properties apply to all
of the text in the text view.
https://developer.apple.com/library/ios/documentation/uikit/reference/uitextview_class/Reference/UITextView.html
When you set the attributes for a specific range of text, it will overwrite any styles that previously existed in that range, using the new attributes only.

How can I fill a unicode symbol in `drawRect` using Core Graphics?

I know what I want to do, but I'm not sure how to achieve it. I'm new to Core Graphics and I'm not sure this is even possible.
I have a unicode symbol like this: ♖
The white parts of this symbol are transparent and I'm drawing it in a transparent rect like so:
- (void)drawRect:(CGRect)rect
{
NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
textStyle.alignment = NSTextAlignmentCenter;
UIFont* myFont = [UIFont fontWithName:#"ArialUnicodeMS" size:80];
NSDictionary *attrs =
#{ NSForegroundColorAttributeName : [UIColor blackColor],
NSFontAttributeName:myFont,
NSParagraphStyleAttributeName:textStyle
};
[[self getStringForValue:_squareValue] drawInRect:rect withAttributes:attrs];
}
What I want is to fill the internal transparent sections with white and leave the surrounding rectangle as transparent.
The approach I'm thinking of is using the unicode symbol as a mask in drawRect to divide the rectangle into regions. Then not filling the region starting at 0,0 but filling all other regions. Can I use Core Graphics to convert the symbol to a mask and then identify and fill specific regions?
What I'm getting at the moment is this:
But what I actually want is this:
You might be able to use -[NSBezierPath appendBezierPathWithGlyph:inFont] and then just set the color to white and call -fill on that path.
EDIT: Since you're on iOS, and I don't know how to get the path of a glyph on iOS, instead I'd just draw the glyph to a buffer that's filled with 100% opaque white, and then do a flood-fill starting from 0,0 with pure transparency.
ANOTHER EDIT: With jrturton's code below, you should be able to trivially do a white fill on the path as originally suggested, then stroke it in black.

Making Glow effect to font

I wonder if anybody have an example for Glow effect for font? I tried to look in NSAttributedString but I don't find in attribute for glowing text.
Try this:
#include <Quartzcore/Quartzcore.h>
.......
yourLabel.layer.shadowColor = [[UIColor glowColor] CGColor]; //Replace glowColor with the desired color (redColor, greenColor, etc.)
yourLabel.layer.shadowOffset = CGSizeMake(0.0, 0.0); //The glow starts from the center
yourLabel.layer.shadowRadius = 20.0; //You can change this to the desired amount
yourLabel.layer.shadowOpacity = 0.5; //You can change this to the desired amount
yourLabel.layer.masksToBounds = NO; //Keep this the same to avoid shadow being clipped by the label bounds
Hope this helps!

Setting up Text Alignment (CTTextAligment) on NSAttributedString - ipad

I'm trying to draw a custom string with color, font, size and alignment.
I got everything working with an NSMutableAttributedString before, but it looks like Text Aligment only works with Paragraph alignement which only works with non mutable version of NSString.
So, I had to change my previous code to this :
//Note : _name variables are provided by my GUI for text, size and font name.
//Create the String ColorRef
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
const CGFloat myColor[] = {_color.r/255.0, _color.g/255.0, _color.b/255.0, 1.0f};
CGColorRef colorRef = CGColorCreate(rgb, myColor);
//Setup paragraph Alignment Ref
CTTextAlignment theAlignment = kCTCenterTextAlignment;
CFIndex theNumberOfSettings = 1;
CTParagraphStyleSetting theSettings[1] = {{ kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &theAlignment }};
CTParagraphStyleRef theParagraphRef = CTParagraphStyleCreate(theSettings, theNumberOfSettings);
//Prep Font
NSDictionary *fontAttributes = [NSDictionary dictionaryWithObjectsAndKeys: _fontName, (NSString *)kCTFontFamilyNameAttribute,
[NSNumber numberWithFloat:_fontSize], (NSString *)kCTFontSizeAttribute,
nil];
CTFontRef font = [self newFontWithAttributes:fontAttributes];
//Prepare String Attributes
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: (id)font, (NSString *)kCTFontAttributeName,
[NSNumber numberWithFloat:_fontSize], (NSString *)kCTFontSizeAttribute,
(id)theParagraphRef, (NSString*)kCTParagraphStyleAttributeName,
colorRef, (NSString *)kCTForegroundColorAttributeName, nil];
//Create the Attributed String
NSAttributedString *myString = [[NSAttributedString alloc] initWithString:_textString
attributes: attributes];
But the Text Aligment still doesn't work. Everything else is fine but text remains aligned on the Left.
Why ?
EDIT :
Each of my strings are created inside a class that is a subclass of CATextLayer. Each of those TextLayers are then added to a CALayer as sublayers. On updates I apply trasformation matrix on the sublayers and use setNeedsDisplay. This is how I display the text on screen. Maybe There's a reason here why the CTParagraphStyleRef set is not working ?
I have no clues why the ParagraphStyle that I've set is not working, But I've found a solution that's working for me, so I'm posting it in case someone encounter similar problems :
My class is subclassing CATextLayer, which I think would've been important to mention in my question (my bad, I'll edit it).
Inside my CATextLayer class, I create the string using the code
shown in my question.
Then I use the self.alignmentMode = kCAAlignmentCenter; to align
the text the way I want.
Each string is then added to a CALAyer for display
I've also found this very good guide on AttributedStrings that helped me improving my code and finding this solution.

Resources