Text add strikethrough and underline

This commit is contained in:
pengfei.zhou 2020-04-13 17:41:17 +08:00 committed by osborn
parent 8faf0e5d33
commit 8cb135b7b1
13 changed files with 151 additions and 15 deletions

View File

@ -143,6 +143,16 @@ public class TextNode extends ViewNode<TextView> {
} }
view.setLineSpacing(DoricUtils.dp2px(prop.asNumber().toFloat()), 1); view.setLineSpacing(DoricUtils.dp2px(prop.asNumber().toFloat()), 1);
break; break;
case "strikethrough":
if (prop.isBoolean()) {
view.getPaint().setStrikeThruText(prop.asBoolean().value());
}
break;
case "underline":
if (prop.isBoolean()) {
view.getPaint().setUnderlineText(prop.asBoolean().value());
}
break;
default: default:
super.blend(view, name, prop); super.blend(view, name, prop);
break; break;

View File

@ -46,6 +46,36 @@ class LayoutDemo extends Panel {
}, },
height: 50, height: 50,
}), }),
image({
imageBase64: icon_refresh,
scaleType: ScaleType.ScaleAspectFit,
backgroundColor: Color.GRAY,
layoutConfig: {
widthSpec: LayoutSpec.FIT,
heightSpec: LayoutSpec.JUST,
},
height: 50,
}),
image({
imageBase64: icon_refresh,
scaleType: ScaleType.ScaleAspectFit,
backgroundColor: Color.GRAY,
layoutConfig: {
widthSpec: LayoutSpec.FIT,
heightSpec: LayoutSpec.JUST,
},
height: 50,
}),
image({
imageBase64: icon_refresh,
scaleType: ScaleType.ScaleAspectFit,
backgroundColor: Color.GRAY,
layoutConfig: {
widthSpec: LayoutSpec.FIT,
heightSpec: LayoutSpec.JUST,
},
height: 50,
}),
image({ image({
imageBase64: icon_refresh, imageBase64: icon_refresh,
scaleType: ScaleType.ScaleAspectFit, scaleType: ScaleType.ScaleAspectFit,
@ -75,9 +105,10 @@ class LayoutDemo extends Panel {
flexConfig: { flexConfig: {
flexDirection: FlexDirection.ROW, flexDirection: FlexDirection.ROW,
width: 200, width: 200,
height: 100, height: 200,
flexWrap: Wrap.WRAP, flexWrap: Wrap.WRAP,
alignContent: Align.CENTER, alignContent: Align.FLEX_END,
alignItems: Align.FLEX_START,
}, },
backgroundColor: Color.GRAY.alpha(0.1), backgroundColor: Color.GRAY.alpha(0.1),
} }

View File

@ -69,6 +69,18 @@ class TextDemo extends Panel {
(this as Text).textColor = Color.BLACK; (this as Text).textColor = Color.BLACK;
} }
}), }),
text({
text: "This is strikethrough text.",
textSize: 20,
textColor: Color.RED,
strikethrough: true,
}),
text({
text: "This is underline text.",
textSize: 20,
textColor: Color.BLUE,
underline: true,
}),
], ],
{ {
space: 10, space: 10,

View File

@ -42,6 +42,8 @@ - (void)drawTextInRect:(CGRect)rect {
@interface DoricTextNode () @interface DoricTextNode ()
@property(nonatomic, strong) NSMutableParagraphStyle *paragraphStyle; @property(nonatomic, strong) NSMutableParagraphStyle *paragraphStyle;
@property(nonatomic, assign) NSNumber *underline;
@property(nonatomic, assign) NSNumber *strikethrough;
@end @end
@implementation DoricTextNode @implementation DoricTextNode
@ -53,12 +55,9 @@ - (UILabel *)build {
- (void)blendView:(UILabel *)view forPropName:(NSString *)name propValue:(id)prop { - (void)blendView:(UILabel *)view forPropName:(NSString *)name propValue:(id)prop {
if ([name isEqualToString:@"text"]) { if ([name isEqualToString:@"text"]) {
view.text = prop;
if (self.paragraphStyle) { if (self.paragraphStyle) {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:prop]; [self reloadParagraphStyle];
[attributedString addAttribute:NSParagraphStyleAttributeName value:self.paragraphStyle range:NSMakeRange(0, [attributedString length])];
view.attributedText = attributedString;
} else {
view.text = prop;
} }
} else if ([name isEqualToString:@"textSize"]) { } else if ([name isEqualToString:@"textSize"]) {
UIFont *font = view.font; UIFont *font = view.font;
@ -113,18 +112,46 @@ - (void)blendView:(UILabel *)view forPropName:(NSString *)name propValue:(id)pro
UIFont *font = [UIFont fontWithName:iconfont size:view.font.pointSize]; UIFont *font = [UIFont fontWithName:iconfont size:view.font.pointSize];
view.font = font; view.font = font;
} else if ([name isEqualToString:@"lineSpacing"]) { } else if ([name isEqualToString:@"lineSpacing"]) {
self.paragraphStyle = [NSMutableParagraphStyle new]; [[self ensureParagraphStyle] also:^(NSMutableParagraphStyle *it) {
[self.paragraphStyle setLineSpacing:[prop floatValue]]; [it setLineSpacing:[prop floatValue]];
[self.paragraphStyle setAlignment:view.textAlignment]; [self reloadParagraphStyle];
NSString *labelText = view.text ?: @""; }];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText]; } else if ([name isEqualToString:@"underline"]) {
[attributedString addAttribute:NSParagraphStyleAttributeName value:self.paragraphStyle range:NSMakeRange(0, [labelText length])]; [[self ensureParagraphStyle] also:^(NSMutableParagraphStyle *it) {
view.attributedText = attributedString; self.underline = prop;
[self reloadParagraphStyle];
}];
} else if ([name isEqualToString:@"strikethrough"]) {
[[self ensureParagraphStyle] also:^(NSMutableParagraphStyle *it) {
self.strikethrough = prop;
[self reloadParagraphStyle];
}];
} else { } else {
[super blendView:view forPropName:name propValue:prop]; [super blendView:view forPropName:name propValue:prop];
} }
} }
- (NSMutableParagraphStyle *)ensureParagraphStyle {
if (self.paragraphStyle == nil) {
self.paragraphStyle = [NSMutableParagraphStyle new];
self.paragraphStyle.alignment = self.view.textAlignment;
}
return self.paragraphStyle;
}
- (void)reloadParagraphStyle {
NSString *labelText = self.view.text ?: @"";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
[attributedString addAttribute:NSParagraphStyleAttributeName value:self.paragraphStyle range:NSMakeRange(0, [labelText length])];
if (self.underline) {
[attributedString addAttribute:NSUnderlineStyleAttributeName value:self.underline range:NSMakeRange(0, [labelText length])];
}
if (self.strikethrough) {
[attributedString addAttribute:NSStrikethroughStyleAttributeName value:self.strikethrough range:NSMakeRange(0, [labelText length])];
}
self.view.attributedText = attributedString;
}
- (void)blend:(NSDictionary *)props { - (void)blend:(NSDictionary *)props {
[super blend:props]; [super blend:props];
[self.view.superview setNeedsLayout]; [self.view.superview setNeedsLayout];

View File

@ -1682,6 +1682,14 @@ var Text = /** @class */ (function (_super) {
Property, Property,
__metadata$3("design:type", Number) __metadata$3("design:type", Number)
], Text.prototype, "lineSpacing", void 0); ], Text.prototype, "lineSpacing", void 0);
__decorate$3([
Property,
__metadata$3("design:type", Boolean)
], Text.prototype, "strikethrough", void 0);
__decorate$3([
Property,
__metadata$3("design:type", Boolean)
], Text.prototype, "underline", void 0);
return Text; return Text;
}(View)); }(View));
function text(config) { function text(config) {

View File

@ -1250,6 +1250,14 @@ __decorate$3([
Property, Property,
__metadata$3("design:type", Number) __metadata$3("design:type", Number)
], Text.prototype, "lineSpacing", void 0); ], Text.prototype, "lineSpacing", void 0);
__decorate$3([
Property,
__metadata$3("design:type", Boolean)
], Text.prototype, "strikethrough", void 0);
__decorate$3([
Property,
__metadata$3("design:type", Boolean)
], Text.prototype, "underline", void 0);
function text(config) { function text(config) {
const ret = new Text; const ret = new Text;
ret.layoutConfig = layoutConfig().fit(); ret.layoutConfig = layoutConfig().fit();

View File

@ -2709,6 +2709,14 @@ __decorate$3([
Property, Property,
__metadata$3("design:type", Number) __metadata$3("design:type", Number)
], Text.prototype, "lineSpacing", void 0); ], Text.prototype, "lineSpacing", void 0);
__decorate$3([
Property,
__metadata$3("design:type", Boolean)
], Text.prototype, "strikethrough", void 0);
__decorate$3([
Property,
__metadata$3("design:type", Boolean)
], Text.prototype, "underline", void 0);
function text(config) { function text(config) {
const ret = new Text; const ret = new Text;
ret.layoutConfig = layoutConfig().fit(); ret.layoutConfig = layoutConfig().fit();

4
doric-js/index.d.ts vendored
View File

@ -484,6 +484,8 @@ declare module 'doric/lib/src/widget/text' {
maxWidth?: number; maxWidth?: number;
maxHeight?: number; maxHeight?: number;
lineSpacing?: number; lineSpacing?: number;
strikethrough?: boolean;
underline?: boolean;
} }
export class Text extends View implements IText { export class Text extends View implements IText {
text?: string; text?: string;
@ -496,6 +498,8 @@ declare module 'doric/lib/src/widget/text' {
maxWidth?: number; maxWidth?: number;
maxHeight?: number; maxHeight?: number;
lineSpacing?: number; lineSpacing?: number;
strikethrough?: boolean;
underline?: boolean;
} }
export function text(config: IText): Text; export function text(config: IText): Text;
} }

View File

@ -12,6 +12,8 @@ export interface IText extends IView {
maxWidth?: number; maxWidth?: number;
maxHeight?: number; maxHeight?: number;
lineSpacing?: number; lineSpacing?: number;
strikethrough?: boolean;
underline?: boolean;
} }
export declare class Text extends View implements IText { export declare class Text extends View implements IText {
text?: string; text?: string;
@ -24,5 +26,7 @@ export declare class Text extends View implements IText {
maxWidth?: number; maxWidth?: number;
maxHeight?: number; maxHeight?: number;
lineSpacing?: number; lineSpacing?: number;
strikethrough?: boolean;
underline?: boolean;
} }
export declare function text(config: IText): Text; export declare function text(config: IText): Text;

View File

@ -68,6 +68,14 @@ __decorate([
Property, Property,
__metadata("design:type", Number) __metadata("design:type", Number)
], Text.prototype, "lineSpacing", void 0); ], Text.prototype, "lineSpacing", void 0);
__decorate([
Property,
__metadata("design:type", Boolean)
], Text.prototype, "strikethrough", void 0);
__decorate([
Property,
__metadata("design:type", Boolean)
], Text.prototype, "underline", void 0);
export function text(config) { export function text(config) {
const ret = new Text; const ret = new Text;
ret.layoutConfig = layoutConfig().fit(); ret.layoutConfig = layoutConfig().fit();

View File

@ -29,6 +29,8 @@ export interface IText extends IView {
maxWidth?: number maxWidth?: number
maxHeight?: number maxHeight?: number
lineSpacing?: number lineSpacing?: number
strikethrough?: boolean
underline?: boolean
} }
export class Text extends View implements IText { export class Text extends View implements IText {
@ -61,6 +63,12 @@ export class Text extends View implements IText {
@Property @Property
lineSpacing?: number lineSpacing?: number
@Property
strikethrough?: boolean
@Property
underline?: boolean
} }
export function text(config: IText) { export function text(config: IText) {

View File

@ -2767,6 +2767,14 @@ __decorate$3([
Property, Property,
__metadata$3("design:type", Number) __metadata$3("design:type", Number)
], Text.prototype, "lineSpacing", void 0); ], Text.prototype, "lineSpacing", void 0);
__decorate$3([
Property,
__metadata$3("design:type", Boolean)
], Text.prototype, "strikethrough", void 0);
__decorate$3([
Property,
__metadata$3("design:type", Boolean)
], Text.prototype, "underline", void 0);
function text(config) { function text(config) {
const ret = new Text; const ret = new Text;
ret.layoutConfig = layoutConfig().fit(); ret.layoutConfig = layoutConfig().fit();

File diff suppressed because one or more lines are too long