feat:add Text lineSpacing
This commit is contained in:
parent
a25aedcc6f
commit
0cfbef67a2
@ -23,6 +23,7 @@ import android.widget.TextView;
|
||||
|
||||
import com.github.pengfeizhou.jscore.JSValue;
|
||||
|
||||
import pub.doric.Doric;
|
||||
import pub.doric.DoricContext;
|
||||
import pub.doric.extension.bridge.DoricPlugin;
|
||||
import pub.doric.utils.DoricUtils;
|
||||
@ -51,6 +52,7 @@ public class TextNode extends ViewNode<TextView> {
|
||||
protected void blend(TextView view, String name, JSValue prop) {
|
||||
switch (name) {
|
||||
case "text":
|
||||
view.setLineSpacing(0, 1);
|
||||
view.setText(prop.asString().toString());
|
||||
break;
|
||||
case "textSize":
|
||||
@ -63,7 +65,11 @@ public class TextNode extends ViewNode<TextView> {
|
||||
view.setGravity(prop.asNumber().toInt() | Gravity.CENTER_VERTICAL);
|
||||
break;
|
||||
case "maxLines":
|
||||
view.setMaxLines(prop.asNumber().toInt());
|
||||
int line = prop.asNumber().toInt();
|
||||
if (line <= 0) {
|
||||
line = Integer.MAX_VALUE;
|
||||
}
|
||||
view.setMaxLines(line);
|
||||
break;
|
||||
case "fontStyle":
|
||||
if (prop.isString()) {
|
||||
@ -97,6 +103,9 @@ public class TextNode extends ViewNode<TextView> {
|
||||
case "maxHeight":
|
||||
view.setMaxHeight(DoricUtils.dp2px(prop.asNumber().toFloat()));
|
||||
break;
|
||||
case "lineSpacing":
|
||||
view.setLineSpacing(DoricUtils.dp2px(prop.asNumber().toFloat()), 1);
|
||||
break;
|
||||
default:
|
||||
super.blend(view, name, prop);
|
||||
break;
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Panel, Group, scroller, vlayout, layoutConfig, LayoutSpec, Input, Gravity, log, input, text } from "doric";
|
||||
import { Panel, Group, scroller, vlayout, layoutConfig, LayoutSpec, Input, Gravity, log, input, text, Color } from "doric";
|
||||
import { title } from "./utils";
|
||||
@Entry
|
||||
class TextDemo extends Panel {
|
||||
@ -54,6 +54,17 @@ class TextDemo extends Panel {
|
||||
textSize: 30,
|
||||
font: 'iconfont'
|
||||
}),
|
||||
text({
|
||||
text: "This is line Spaceing 0,\nSecond line",
|
||||
maxLines: 0,
|
||||
}),
|
||||
text({
|
||||
text: "This is line Spaceing 40,\nSecond line",
|
||||
maxLines: 0,
|
||||
lineSpacing: 40,
|
||||
textColor: Color.RED,
|
||||
textAlignment: Gravity.Right,
|
||||
}),
|
||||
],
|
||||
{
|
||||
space: 10,
|
||||
|
@ -49,6 +49,10 @@ - (CGSize)measureSize:(CGSize)targetSize {
|
||||
}
|
||||
@end
|
||||
|
||||
@interface DoricTextNode ()
|
||||
@property(nonatomic, strong) NSMutableParagraphStyle *paragraphStyle;
|
||||
@end
|
||||
|
||||
@implementation DoricTextNode
|
||||
- (UILabel *)build {
|
||||
return [[[DoricTextView alloc] init] also:^(UILabel *it) {
|
||||
@ -58,7 +62,13 @@ - (UILabel *)build {
|
||||
|
||||
- (void)blendView:(UILabel *)view forPropName:(NSString *)name propValue:(id)prop {
|
||||
if ([name isEqualToString:@"text"]) {
|
||||
view.text = prop;
|
||||
if (self.paragraphStyle) {
|
||||
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:prop];
|
||||
[attributedString addAttribute:NSParagraphStyleAttributeName value:self.paragraphStyle range:NSMakeRange(0, [attributedString length])];
|
||||
view.attributedText = attributedString;
|
||||
} else {
|
||||
view.text = prop;
|
||||
}
|
||||
} else if ([name isEqualToString:@"textSize"]) {
|
||||
UIFont *font = view.font;
|
||||
if (font) {
|
||||
@ -76,7 +86,11 @@ - (void)blendView:(UILabel *)view forPropName:(NSString *)name propValue:(id)pro
|
||||
} else if ((gravity & RIGHT) == RIGHT) {
|
||||
alignment = NSTextAlignmentRight;
|
||||
}
|
||||
view.textAlignment = alignment;
|
||||
if (self.paragraphStyle) {
|
||||
self.paragraphStyle.alignment = alignment;
|
||||
} else {
|
||||
view.textAlignment = alignment;
|
||||
}
|
||||
} else if ([name isEqualToString:@"maxLines"]) {
|
||||
view.numberOfLines = [prop integerValue];
|
||||
} else if ([name isEqualToString:@"fontStyle"]) {
|
||||
@ -108,6 +122,14 @@ - (void)blendView:(UILabel *)view forPropName:(NSString *)name propValue:(id)pro
|
||||
NSString *iconfont = prop;
|
||||
UIFont *font = [UIFont fontWithName:iconfont size:view.font.pointSize];
|
||||
view.font = font;
|
||||
} else if ([name isEqualToString:@"lineSpacing"]) {
|
||||
self.paragraphStyle = [NSMutableParagraphStyle new];
|
||||
[self.paragraphStyle setLineSpacing:[prop floatValue]];
|
||||
[self.paragraphStyle setAlignment:view.textAlignment];
|
||||
NSString *labelText = view.text ?: @"";
|
||||
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
|
||||
[attributedString addAttribute:NSParagraphStyleAttributeName value:self.paragraphStyle range:NSMakeRange(0, [labelText length])];
|
||||
view.attributedText = attributedString;
|
||||
} else {
|
||||
[super blendView:view forPropName:name propValue:prop];
|
||||
}
|
||||
|
@ -1628,6 +1628,10 @@ var Text = /** @class */ (function (_super) {
|
||||
Property,
|
||||
__metadata$3("design:type", Number)
|
||||
], Text.prototype, "maxHeight", void 0);
|
||||
__decorate$3([
|
||||
Property,
|
||||
__metadata$3("design:type", Number)
|
||||
], Text.prototype, "lineSpacing", void 0);
|
||||
return Text;
|
||||
}(View));
|
||||
function text(config) {
|
||||
|
@ -1212,6 +1212,10 @@ __decorate$3([
|
||||
Property,
|
||||
__metadata$3("design:type", Number)
|
||||
], Text.prototype, "maxHeight", void 0);
|
||||
__decorate$3([
|
||||
Property,
|
||||
__metadata$3("design:type", Number)
|
||||
], Text.prototype, "lineSpacing", void 0);
|
||||
function text(config) {
|
||||
const ret = new Text;
|
||||
ret.layoutConfig = layoutConfig().fit();
|
||||
|
@ -2671,6 +2671,10 @@ __decorate$3([
|
||||
Property,
|
||||
__metadata$3("design:type", Number)
|
||||
], Text.prototype, "maxHeight", void 0);
|
||||
__decorate$3([
|
||||
Property,
|
||||
__metadata$3("design:type", Number)
|
||||
], Text.prototype, "lineSpacing", void 0);
|
||||
function text(config) {
|
||||
const ret = new Text;
|
||||
ret.layoutConfig = layoutConfig().fit();
|
||||
|
2
doric-js/index.d.ts
vendored
2
doric-js/index.d.ts
vendored
@ -474,6 +474,7 @@ declare module 'doric/lib/src/widget/text' {
|
||||
font?: string;
|
||||
maxWidth?: number;
|
||||
maxHeight?: number;
|
||||
lineSpacing?: number;
|
||||
}
|
||||
export class Text extends View implements IText {
|
||||
text?: string;
|
||||
@ -485,6 +486,7 @@ declare module 'doric/lib/src/widget/text' {
|
||||
font?: string;
|
||||
maxWidth?: number;
|
||||
maxHeight?: number;
|
||||
lineSpacing?: number;
|
||||
}
|
||||
export function text(config: IText): Text;
|
||||
}
|
||||
|
2
doric-js/lib/src/widget/text.d.ts
vendored
2
doric-js/lib/src/widget/text.d.ts
vendored
@ -11,6 +11,7 @@ export interface IText extends IView {
|
||||
font?: string;
|
||||
maxWidth?: number;
|
||||
maxHeight?: number;
|
||||
lineSpacing?: number;
|
||||
}
|
||||
export declare class Text extends View implements IText {
|
||||
text?: string;
|
||||
@ -22,5 +23,6 @@ export declare class Text extends View implements IText {
|
||||
font?: string;
|
||||
maxWidth?: number;
|
||||
maxHeight?: number;
|
||||
lineSpacing?: number;
|
||||
}
|
||||
export declare function text(config: IText): Text;
|
||||
|
@ -64,6 +64,10 @@ __decorate([
|
||||
Property,
|
||||
__metadata("design:type", Number)
|
||||
], Text.prototype, "maxHeight", void 0);
|
||||
__decorate([
|
||||
Property,
|
||||
__metadata("design:type", Number)
|
||||
], Text.prototype, "lineSpacing", void 0);
|
||||
export function text(config) {
|
||||
const ret = new Text;
|
||||
ret.layoutConfig = layoutConfig().fit();
|
||||
|
@ -28,6 +28,7 @@ export interface IText extends IView {
|
||||
font?: string
|
||||
maxWidth?: number
|
||||
maxHeight?: number
|
||||
lineSpacing?: number
|
||||
}
|
||||
|
||||
export class Text extends View implements IText {
|
||||
@ -57,6 +58,9 @@ export class Text extends View implements IText {
|
||||
|
||||
@Property
|
||||
maxHeight?: number
|
||||
|
||||
@Property
|
||||
lineSpacing?: number
|
||||
}
|
||||
|
||||
export function text(config: IText) {
|
||||
|
4
doric-web/dist/index.js
vendored
4
doric-web/dist/index.js
vendored
@ -2729,6 +2729,10 @@ __decorate$3([
|
||||
Property,
|
||||
__metadata$3("design:type", Number)
|
||||
], Text.prototype, "maxHeight", void 0);
|
||||
__decorate$3([
|
||||
Property,
|
||||
__metadata$3("design:type", Number)
|
||||
], Text.prototype, "lineSpacing", void 0);
|
||||
function text(config) {
|
||||
const ret = new Text;
|
||||
ret.layoutConfig = layoutConfig().fit();
|
||||
|
2
doric-web/dist/index.js.map
vendored
2
doric-web/dist/index.js.map
vendored
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user