Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
Fix Chinese text italic property
Browse files Browse the repository at this point in the history
  • Loading branch information
Txink committed Jul 18, 2019
1 parent 1ee8c74 commit 5bcbe6d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
19 changes: 15 additions & 4 deletions ios/sdk/WeexSDK/Sources/Component/WXTextComponent.mm
Original file line number Diff line number Diff line change
Expand Up @@ -499,10 +499,15 @@ - (NSMutableAttributedString *)buildCTAttributeString
}

// set font
UIFont *font = [WXUtility fontWithSize:_fontSize textWeight:_fontWeight textStyle:_fontStyle fontFamily:_fontFamily scaleFactor:self.weexInstance.pixelScaleFactor useCoreText:[self useCoreText]];
CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)font.fontName,
font.pointSize,
NULL);
UIFont *font = [WXUtility fontWithSize:_fontSize textWeight:_fontWeight textStyle:WXTextStyleNormal fontFamily:_fontFamily scaleFactor:self.weexInstance.pixelScaleFactor useCoreText:[self useCoreText]];
CTFontRef ctFont;

if (_fontStyle == WXTextStyleItalic) {
CGAffineTransform matrix = CGAffineTransformMake(1, 0, tanf(16 * (CGFloat)M_PI / 180), 1, 0, 0);
ctFont = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, &matrix);
}else {
ctFont = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, NULL);
}

_fontAscender = font.ascender;
_fontDescender = font.descender;
Expand Down Expand Up @@ -870,6 +875,12 @@ - (void)drawTextWithRuns:(CFArrayRef)runs context:(CGContextRef)context lineOrig
lineOrigin.y += [baselineOffset doubleValue];
}
}
//To properly draw the glyphs in a run, the fields tx and ty of the CGAffineTransform returned by CTRunGetTextMatrix should be set to the current text position.
CGAffineTransform transform = CTRunGetTextMatrix(run);
transform.tx = lineOrigin.x;
transform.ty = lineOrigin.y;
CGContextSetTextMatrix(context, transform);

CGContextSetTextPosition(context, lineOrigin.x, lineOrigin.y);
CTRunDraw(run, context, CFRangeMake(0, 0));
CFIndex glyphCount = CTRunGetGlyphCount(run);
Expand Down
18 changes: 14 additions & 4 deletions ios/sdk/WeexSDK/Sources/Utility/WXUtility.m
Original file line number Diff line number Diff line change
Expand Up @@ -607,10 +607,20 @@ + (UIFont *)fontWithSize:(CGFloat)size textWeight:(CGFloat)textWeight textStyle:
UIFontDescriptor *fontD = font.fontDescriptor;
UIFontDescriptorSymbolicTraits traits = 0;

traits = (textStyle == WXTextStyleItalic) ? (traits | UIFontDescriptorTraitItalic) : traits;
traits = (textWeight-UIFontWeightBold >= 0.0) ? (traits | UIFontDescriptorTraitBold) : traits;
if (traits != 0) {
fontD = [fontD fontDescriptorWithSymbolicTraits:traits];
if (WX_SYS_VERSION_LESS_THAN(@"8.2")) {
traits = ((textWeight-0.4) >= 0.0) ? (traits | UIFontDescriptorTraitBold) : traits;
}else {
traits = (textWeight-UIFontWeightBold >= 0.0) ? (traits | UIFontDescriptorTraitBold) : traits;
}

if (textStyle == WXTextStyleItalic || traits != 0) {
if (traits != 0) {
fontD = [fontD fontDescriptorWithSymbolicTraits:traits];
}
if (textStyle == WXTextStyleItalic) {
CGAffineTransform matrix = CGAffineTransformMake(1, 0, tanf(16 * (CGFloat)M_PI / 180), 1, 0, 0);
fontD = [fontD fontDescriptorWithMatrix:matrix];
}
UIFont *tempFont = [UIFont fontWithDescriptor:fontD size:0];
if (tempFont) {
font = tempFont;
Expand Down

0 comments on commit 5bcbe6d

Please sign in to comment.