iOS text font support from custom loader.

This commit is contained in:
吴尚昆 2022-03-02 17:09:26 +08:00 committed by osborn
parent 92bbe90165
commit 93e949ef41
11 changed files with 3163 additions and 1697 deletions

View File

@ -1,4 +1,4 @@
import { Panel, Group, scroller, vlayout, layoutConfig, LayoutSpec, Input, Gravity, log, input, text, Color, Text, InputType, hlayout, GradientOrientation } from "doric"; import { Panel, Group, scroller, vlayout, layoutConfig, LayoutSpec, Input, Gravity, log, input, text, Color, Text, InputType, hlayout, GradientOrientation, AssetsResource } from "doric";
import { title } from "./utils"; import { title } from "./utils";
@Entry @Entry
class TextDemo extends Panel { class TextDemo extends Panel {
@ -207,6 +207,17 @@ class TextDemo extends Panel {
textSize: 30, textSize: 30,
font: 'assets/fonts/assets_iconfont.ttf' font: 'assets/fonts/assets_iconfont.ttf'
}), }),
text({
text: "Font from custom loader.",
textSize: 10,
font: 'Hanabi'
}),
text({
text: "Font from custom loader.",
textSize: 30,
textColor: Color.BLUE,
font: new AssetsResource('Hanabi.ttf')
}),
text({ text({
text: "This is line Spaceing 0,\nSecond line", text: "This is line Spaceing 0,\nSecond line",
maxLines: 0, maxLines: 0,

View File

@ -24,6 +24,7 @@
#import "DoricUtil.h" #import "DoricUtil.h"
#import "DoricGroupNode.h" #import "DoricGroupNode.h"
#import "Doric.h" #import "Doric.h"
#import <CoreText/CoreText.h>
@interface DoricTextView : UILabel @interface DoricTextView : UILabel
@property(nonatomic, assign) DoricGravity gravity; @property(nonatomic, assign) DoricGravity gravity;
@ -60,6 +61,7 @@ @interface DoricTextNode ()
@property(nonatomic, copy) NSNumber *strikethrough; @property(nonatomic, copy) NSNumber *strikethrough;
@property(nonatomic, strong) NSDictionary *textGradientProps; @property(nonatomic, strong) NSDictionary *textGradientProps;
@property(nonatomic, assign) CGSize textGradientSize; @property(nonatomic, assign) CGSize textGradientSize;
@property(nonatomic, assign) CGFloat textSize;
@end @end
@implementation DoricTextNode @implementation DoricTextNode
@ -82,6 +84,7 @@ - (void)blendView:(UILabel *)view forPropName:(NSString *)name propValue:(id)pro
} else { } else {
view.font = [UIFont systemFontOfSize:[(NSNumber *) prop floatValue]]; view.font = [UIFont systemFontOfSize:[(NSNumber *) prop floatValue]];
} }
self.textSize = [(NSNumber *) prop floatValue];
} else if ([name isEqualToString:@"textColor"]) { } else if ([name isEqualToString:@"textColor"]) {
if ([prop isKindOfClass:[NSNumber class]]) { if ([prop isKindOfClass:[NSNumber class]]) {
view.textColor = DoricColor(prop); view.textColor = DoricColor(prop);
@ -134,10 +137,24 @@ - (void)blendView:(UILabel *)view forPropName:(NSString *)name propValue:(id)pro
} else if ([name isEqualToString:@"maxHeight"]) { } else if ([name isEqualToString:@"maxHeight"]) {
view.doricLayout.maxHeight = [prop floatValue]; view.doricLayout.maxHeight = [prop floatValue];
} else if ([name isEqualToString:@"font"]) { } else if ([name isEqualToString:@"font"]) {
NSString *iconfont = prop; if ([prop isKindOfClass:[NSString class]]) {
UIFont *font = [UIFont fontWithName:[iconfont stringByReplacingOccurrencesOfString:@".ttf" withString:@""] NSString *iconfont = prop;
size:view.font.pointSize]; UIFont *font = [UIFont fontWithName:[iconfont stringByReplacingOccurrencesOfString:@".ttf" withString:@""]
view.font = font; size:view.font.pointSize];
view.font = font;
} else if ([prop isKindOfClass:[NSDictionary class]]) {
DoricAsyncResult <NSData *> *asyncResult = [[self.doricContext.driver.registry.loaderManager
load:prop
withContext:self.doricContext] fetch];
[asyncResult setResultCallback:^(NSData *fontData) {
[self.doricContext dispatchToMainQueue:^{
view.font = [self registerFontWithFontData:fontData fontSize:self.textSize > 0 ? self.textSize : 12];
}];
}];
[asyncResult setExceptionCallback:^(NSException *e) {
DoricLog(@"Cannot load resource %@, %@", prop, e.reason);
}];
}
} else if ([name isEqualToString:@"lineSpacing"]) { } else if ([name isEqualToString:@"lineSpacing"]) {
[[self ensureParagraphStyle] also:^(NSMutableParagraphStyle *it) { [[self ensureParagraphStyle] also:^(NSMutableParagraphStyle *it) {
[it setLineSpacing:[prop floatValue]]; [it setLineSpacing:[prop floatValue]];
@ -306,6 +323,19 @@ - (void)requestLayout {
}]; }];
} }
- (UIFont *)registerFontWithFontData:(NSData *)fontData fontSize:(CGFloat)fontSize{
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)fontData);
CGFontRef fontRef = CGFontCreateWithDataProvider(fontDataProvider);
[UIFont familyNames];
CGDataProviderRelease(fontDataProvider);
CTFontManagerRegisterGraphicsFont(fontRef, NULL);
NSString *fontName = CFBridgingRelease(CGFontCopyPostScriptName(fontRef));
UIFont *font = [UIFont fontWithName:fontName size:fontSize];
CGFontRelease(fontRef);
return font;
}
- (UIImage *)gradientImageFromColors:(NSArray *)colors - (UIImage *)gradientImageFromColors:(NSArray *)colors
locations:(CGFloat *)locations locations:(CGFloat *)locations
startPoint:(CGPoint)startPoint startPoint:(CGPoint)startPoint

View File

@ -68,7 +68,7 @@ var Mutable = /** @class */ (function () {
*/ */
var __uniqueId__ = 0; var __uniqueId__ = 0;
function uniqueId(prefix) { function uniqueId(prefix) {
return "__" + prefix + "_" + __uniqueId__++ + "__"; return "__".concat(prefix, "_").concat(__uniqueId__++, "__");
} }
function toString(message) { function toString(message) {
@ -177,11 +177,11 @@ var __values$5 = (undefined && undefined.__values) || function(o) {
function Property(target, propKey) { function Property(target, propKey) {
Object.defineProperty(target, propKey, { Object.defineProperty(target, propKey, {
get: function () { get: function () {
return Reflect.get(this, "__prop__" + propKey, this); return Reflect.get(this, "__prop__".concat(propKey), this);
}, },
set: function (v) { set: function (v) {
var oldV = Reflect.get(this, "__prop__" + propKey, this); var oldV = Reflect.get(this, "__prop__".concat(propKey), this);
Reflect.set(this, "__prop__" + propKey, v, this); Reflect.set(this, "__prop__".concat(propKey), v, this);
if (oldV !== v) { if (oldV !== v) {
Reflect.apply(this.onPropertyChanged, this, [propKey, oldV, v]); Reflect.apply(this.onPropertyChanged, this, [propKey, oldV, v]);
} }
@ -191,11 +191,11 @@ function Property(target, propKey) {
function InconsistProperty(target, propKey) { function InconsistProperty(target, propKey) {
Object.defineProperty(target, propKey, { Object.defineProperty(target, propKey, {
get: function () { get: function () {
return Reflect.get(this, "__prop__" + propKey, this); return Reflect.get(this, "__prop__".concat(propKey), this);
}, },
set: function (v) { set: function (v) {
var oldV = Reflect.get(this, "__prop__" + propKey, this); var oldV = Reflect.get(this, "__prop__".concat(propKey), this);
Reflect.set(this, "__prop__" + propKey, v, this); Reflect.set(this, "__prop__".concat(propKey), v, this);
Reflect.apply(this.onPropertyChanged, this, [propKey, oldV, v]); Reflect.apply(this.onPropertyChanged, this, [propKey, oldV, v]);
}, },
}); });
@ -373,7 +373,7 @@ var View = /** @class */ (function () {
return Reflect.apply(f, this, argumentsList); return Reflect.apply(f, this, argumentsList);
} }
else { else {
loge("Cannot find callback:" + id + " for " + JSON.stringify(this.toModel())); loge("Cannot find callback:".concat(id, " for ").concat(JSON.stringify(this.toModel())));
} }
}; };
View.prototype.toModel = function () { View.prototype.toModel = function () {
@ -714,7 +714,7 @@ var Group = /** @class */ (function (_super) {
this.addChild(e); this.addChild(e);
} }
else { else {
loge("Not allowed to add " + typeof e); loge("Not allowed to add ".concat(typeof e));
} }
}; };
Object.defineProperty(Group.prototype, "innerElement", { Object.defineProperty(Group.prototype, "innerElement", {
@ -1225,7 +1225,7 @@ var Panel = /** @class */ (function () {
var v = this.retrospectView(viewIds); var v = this.retrospectView(viewIds);
if (v === undefined) { if (v === undefined) {
loge("Cannot find view for " + viewIds); loge("Cannot find view for ".concat(viewIds));
} }
else { else {
var argumentsList = [callbackId]; var argumentsList = [callbackId];
@ -1427,7 +1427,7 @@ var Color = /** @class */ (function () {
} }
Color.parse = function (str) { Color.parse = function (str) {
if (!str.startsWith("#")) { if (!str.startsWith("#")) {
throw new Error("Parse color error with " + str); throw new Error("Parse color error with ".concat(str));
} }
var val = parseInt(str.substr(1), 16); var val = parseInt(str.substr(1), 16);
if (str.length === 7) { if (str.length === 7) {
@ -1437,7 +1437,7 @@ var Color = /** @class */ (function () {
return new Color(val); return new Color(val);
} }
else { else {
throw new Error("Parse color error with " + str); throw new Error("Parse color error with ".concat(str));
} }
}; };
Color.safeParse = function (str, defVal) { Color.safeParse = function (str, defVal) {
@ -2089,7 +2089,7 @@ var Text = /** @class */ (function (_super) {
], Text.prototype, "fontStyle", void 0); ], Text.prototype, "fontStyle", void 0);
__decorate$c([ __decorate$c([
Property, Property,
__metadata$c("design:type", String) __metadata$c("design:type", Object)
], Text.prototype, "font", void 0); ], Text.prototype, "font", void 0);
__decorate$c([ __decorate$c([
Property, Property,
@ -2240,7 +2240,7 @@ var MainBundleResource = /** @class */ (function (_super) {
var BundleResource = /** @class */ (function (_super) { var BundleResource = /** @class */ (function (_super) {
__extends$f(BundleResource, _super); __extends$f(BundleResource, _super);
function BundleResource(bundleName, fileName) { function BundleResource(bundleName, fileName) {
return _super.call(this, "bundle", bundleName + "://" + fileName) || this; return _super.call(this, "bundle", "".concat(bundleName, "://").concat(fileName)) || this;
} }
return BundleResource; return BundleResource;
}(iOSResource)); }(iOSResource));
@ -2514,7 +2514,7 @@ var List = /** @class */ (function (_super) {
List.prototype.getItem = function (itemIdx) { List.prototype.getItem = function (itemIdx) {
var view = this.renderItem(itemIdx); var view = this.renderItem(itemIdx);
view.superview = this; view.superview = this;
this.cachedViews.set("" + itemIdx, view); this.cachedViews.set("".concat(itemIdx), view);
return view; return view;
}; };
List.prototype.renderBunchedItems = function (start, length) { List.prototype.renderBunchedItems = function (start, length) {
@ -2648,7 +2648,7 @@ var Slider = /** @class */ (function (_super) {
Slider.prototype.getItem = function (itemIdx) { Slider.prototype.getItem = function (itemIdx) {
var view = this.renderPage(itemIdx); var view = this.renderPage(itemIdx);
view.superview = this; view.superview = this;
this.cachedViews.set("" + itemIdx, view); this.cachedViews.set("".concat(itemIdx), view);
return view; return view;
}; };
Slider.prototype.renderBunchedItems = function (start, length) { Slider.prototype.renderBunchedItems = function (start, length) {
@ -3012,7 +3012,7 @@ exports.jsx = void 0;
Reflect.set(e, "innerElement", children, e); Reflect.set(e, "innerElement", children, e);
} }
else { else {
throw new Error("Do not support " + constructor.name + " for " + children); throw new Error("Do not support ".concat(constructor.name, " for ").concat(children));
} }
} }
return e; return e;
@ -3130,7 +3130,7 @@ var FlowLayout = /** @class */ (function (_super) {
FlowLayout.prototype.getItem = function (itemIdx) { FlowLayout.prototype.getItem = function (itemIdx) {
var view = this.renderItem(itemIdx); var view = this.renderItem(itemIdx);
view.superview = this; view.superview = this;
this.cachedViews.set("" + itemIdx, view); this.cachedViews.set("".concat(itemIdx), view);
return view; return view;
}; };
FlowLayout.prototype.renderBunchedItems = function (start, length) { FlowLayout.prototype.renderBunchedItems = function (start, length) {
@ -3811,7 +3811,7 @@ function navbar(context) {
} }
function internalScheme(context, panelClass) { function internalScheme(context, panelClass) {
return "_internal_://export?class=" + encodeURIComponent(panelClass.name) + "&context=" + context.id; return "_internal_://export?class=".concat(encodeURIComponent(panelClass.name), "&context=").concat(context.id);
} }
function navigator(context) { function navigator(context) {
var moduleName = "navigator"; var moduleName = "navigator";
@ -3851,9 +3851,9 @@ function transformRequest(request) {
if (request.params !== undefined) { if (request.params !== undefined) {
var queryStrings = []; var queryStrings = [];
for (var key in request.params) { for (var key in request.params) {
queryStrings.push(key + "=" + encodeURIComponent(request.params[key])); queryStrings.push("".concat(key, "=").concat(encodeURIComponent(request.params[key])));
} }
request.url = "" + request.url + (url.indexOf('?') >= 0 ? '&' : '?') + queryStrings.join('&'); request.url = "".concat(request.url).concat(url.indexOf('?') >= 0 ? '&' : '?').concat(queryStrings.join('&'));
} }
if (typeof request.data === 'object') { if (typeof request.data === 'object') {
request.data = JSON.stringify(request.data); request.data = JSON.stringify(request.data);
@ -4129,7 +4129,7 @@ function animate(context) {
} }
else { else {
return function (args) { return function (args) {
return Promise.reject("Cannot find panel in Context:" + context.id); return Promise.reject("Cannot find panel in Context:".concat(context.id));
}; };
} }
} }

View File

@ -1571,7 +1571,7 @@ __decorate$c([
], Text.prototype, "fontStyle", void 0); ], Text.prototype, "fontStyle", void 0);
__decorate$c([ __decorate$c([
Property, Property,
__metadata$c("design:type", String) __metadata$c("design:type", Object)
], Text.prototype, "font", void 0); ], Text.prototype, "font", void 0);
__decorate$c([ __decorate$c([
Property, Property,

File diff suppressed because it is too large Load Diff

View File

@ -3099,7 +3099,7 @@ __decorate$c([
], Text.prototype, "fontStyle", void 0); ], Text.prototype, "fontStyle", void 0);
__decorate$c([ __decorate$c([
Property, Property,
__metadata$c("design:type", String) __metadata$c("design:type", Object)
], Text.prototype, "font", void 0); ], Text.prototype, "font", void 0);
__decorate$c([ __decorate$c([
Property, Property,

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

@ -590,6 +590,7 @@ declare module 'doric/lib/src/widget/text' {
import { View } from "doric/lib/src/ui/view"; import { View } from "doric/lib/src/ui/view";
import { Color, GradientColor } from "doric/lib/src/util/color"; import { Color, GradientColor } from "doric/lib/src/util/color";
import { Gravity } from "doric/lib/src/util/gravity"; import { Gravity } from "doric/lib/src/util/gravity";
import { Resource } from "doric/lib/src/util/resource";
export enum TruncateAt { export enum TruncateAt {
End = 0, End = 0,
Middle = 1, Middle = 1,
@ -603,7 +604,7 @@ declare module 'doric/lib/src/widget/text' {
maxLines?: number; maxLines?: number;
textAlignment?: Gravity; textAlignment?: Gravity;
fontStyle?: "normal" | "bold" | "italic" | "bold_italic"; fontStyle?: "normal" | "bold" | "italic" | "bold_italic";
font?: string; font?: string | Resource;
maxWidth?: number; maxWidth?: number;
maxHeight?: number; maxHeight?: number;
lineSpacing?: number; lineSpacing?: number;

View File

@ -1,6 +1,7 @@
import { View } from "../ui/view"; import { View } from "../ui/view";
import { Color, GradientColor } from "../util/color"; import { Color, GradientColor } from "../util/color";
import { Gravity } from "../util/gravity"; import { Gravity } from "../util/gravity";
import { Resource } from "../util/resource";
export declare enum TruncateAt { export declare enum TruncateAt {
End = 0, End = 0,
Middle = 1, Middle = 1,
@ -14,7 +15,7 @@ export declare class Text extends View implements JSX.ElementChildrenAttribute {
maxLines?: number; maxLines?: number;
textAlignment?: Gravity; textAlignment?: Gravity;
fontStyle?: "normal" | "bold" | "italic" | "bold_italic"; fontStyle?: "normal" | "bold" | "italic" | "bold_italic";
font?: string; font?: string | Resource;
maxWidth?: number; maxWidth?: number;
maxHeight?: number; maxHeight?: number;
lineSpacing?: number; lineSpacing?: number;

View File

@ -63,7 +63,7 @@ __decorate([
], Text.prototype, "fontStyle", void 0); ], Text.prototype, "fontStyle", void 0);
__decorate([ __decorate([
Property, Property,
__metadata("design:type", String) __metadata("design:type", Object)
], Text.prototype, "font", void 0); ], Text.prototype, "font", void 0);
__decorate([ __decorate([
Property, Property,

View File

@ -17,6 +17,7 @@ import { View, Property } from "../ui/view"
import { Color, GradientColor } from "../util/color" import { Color, GradientColor } from "../util/color"
import { Gravity } from "../util/gravity" import { Gravity } from "../util/gravity"
import { layoutConfig } from "../util/layoutconfig" import { layoutConfig } from "../util/layoutconfig"
import { Resource } from "../util/resource"
export enum TruncateAt { export enum TruncateAt {
End = 0, End = 0,
@ -45,7 +46,7 @@ export class Text extends View implements JSX.ElementChildrenAttribute {
fontStyle?: "normal" | "bold" | "italic" | "bold_italic" fontStyle?: "normal" | "bold" | "italic" | "bold_italic"
@Property @Property
font?: string font?: string | Resource
@Property @Property
maxWidth?: number maxWidth?: number

View File

@ -3173,7 +3173,7 @@ __decorate$c([
], Text.prototype, "fontStyle", void 0); ], Text.prototype, "fontStyle", void 0);
__decorate$c([ __decorate$c([
Property, Property,
__metadata$c("design:type", String) __metadata$c("design:type", Object)
], Text.prototype, "font", void 0); ], Text.prototype, "font", void 0);
__decorate$c([ __decorate$c([
Property, Property,