Skip to content

Commit

Permalink
feat: 评论支持表情显示
Browse files Browse the repository at this point in the history
  • Loading branch information
yichengchen committed Nov 9, 2024
1 parent 220afc9 commit 4677599
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
4 changes: 4 additions & 0 deletions BilibiliLive.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
4947423B2906B308005D6885 /* BLTextOnlyCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4947423A2906B308005D6885 /* BLTextOnlyCollectionViewCell.swift */; };
49508E0F2943420100D26812 /* CocoaLumberjack in Frameworks */ = {isa = PBXBuildFile; productRef = 49508E0E2943420100D26812 /* CocoaLumberjack */; };
49508E112943420100D26812 /* CocoaLumberjackSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 49508E102943420100D26812 /* CocoaLumberjackSwift */; };
495E6E102CDF92AB00689A08 /* Replys+AttritubedString.swift in Sources */ = {isa = PBXBuildFile; fileRef = 495E6E0F2CDF92AB00689A08 /* Replys+AttritubedString.swift */; };
496400D32943431E0098ACA6 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 496400D22943431E0098ACA6 /* Logger.swift */; };
496E5A4F2C0194720062951B /* MaskViewPugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 496E5A4E2C0194720062951B /* MaskViewPugin.swift */; };
496E5A512C0194CD0062951B /* BUpnpPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 496E5A502C0194CD0062951B /* BUpnpPlugin.swift */; };
Expand Down Expand Up @@ -189,6 +190,7 @@
494742102905053E005D6885 /* BLMotionCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BLMotionCollectionViewCell.swift; sourceTree = "<group>"; };
49474212290509F6005D6885 /* DateFormatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateFormatter.swift; sourceTree = "<group>"; };
4947423A2906B308005D6885 /* BLTextOnlyCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BLTextOnlyCollectionViewCell.swift; sourceTree = "<group>"; };
495E6E0F2CDF92AB00689A08 /* Replys+AttritubedString.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Replys+AttritubedString.swift"; sourceTree = "<group>"; };
496400D22943431E0098ACA6 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
496E5A4E2C0194720062951B /* MaskViewPugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MaskViewPugin.swift; sourceTree = "<group>"; };
496E5A502C0194CD0062951B /* BUpnpPlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BUpnpPlugin.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -415,6 +417,7 @@
492AD70C2BFF33DF007221C8 /* VideoPlayerViewController.swift */,
492AD70E2BFF6761007221C8 /* NewVideoPlayerViewModel.swift */,
497CF2332C16EDC0006E1488 /* Plugins */,
495E6E0F2CDF92AB00689A08 /* Replys+AttritubedString.swift */,
);
path = Video;
sourceTree = "<group>";
Expand Down Expand Up @@ -962,6 +965,7 @@
498CF2972B63AABE0009793E /* encode.c in Sources */,
49389D8C28B0A84500B9DAFD /* PersonalViewController.swift in Sources */,
498CF2992B63AABE0009793E /* cluster.c in Sources */,
495E6E102CDF92AB00689A08 /* Replys+AttritubedString.swift in Sources */,
498CF2952B63AABE0009793E /* compress_fragment_two_pass.c in Sources */,
F927ED772610395300EAB8E3 /* DanmakuQueuePool.swift in Sources */,
497CF2382C16EDE5006E1488 /* BVideoInfoPlugin.swift in Sources */,
Expand Down
5 changes: 5 additions & 0 deletions BilibiliLive/Component/Video/ReplyDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class ReplyDetailViewController: UIViewController {

setUpViews()
replyLabel.setTitle(reply.content.message, for: .normal)

if let attr = reply.createAttributedString(displayView: replyLabel) {
replyLabel.setAttributedTitle(attr, for: .normal)
}

reply.content.pictures?.compactMap { URL(string: $0.img_src) }.forEach { url in
let imageView = UIImageView()
imageView.kf.setImage(with: url)
Expand Down
32 changes: 32 additions & 0 deletions BilibiliLive/Component/Video/Replys+AttritubedString.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Replys+AttritubedString.swift
// BilibiliLive
//
// Created by yicheng on 9/11/2024.
//

import Kingfisher
import UIKit

extension Replys.Reply {
func createAttributedString(displayView: UIView) -> NSAttributedString? {
guard let emote = content.emote, !emote.isEmpty else {
return nil
}
let attr = NSMutableAttributedString(string: content.message)
for (tag, emote) in emote {
guard let url = URL(string: emote.url) else { continue }
let ranges = attr.string.ranges(of: tag).reversed()
for range in ranges {
let textAttachment = NSTextAttachment()
attr.replaceCharacters(in: NSRange(range, in: attr.string), with: NSAttributedString(attachment: textAttachment))
// TODO: 文本对其,添加间距
KF.url(url)
.resizing(referenceSize: CGSize(width: 36, height: 36))
.roundCorner(radius: .point(15))
.set(to: textAttachment, attributedView: displayView)
}
}
return attr
}
}
6 changes: 5 additions & 1 deletion BilibiliLive/Component/View/ReplyCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class ReplyCell: UICollectionViewCell {
]
)
userNameLabel.text = replay.member.uname
contenLabel.text = replay.content.message
if let attr = replay.createAttributedString(displayView: contenLabel) {
contenLabel.attributedText = attr
} else {
contenLabel.text = replay.content.message
}
}
}
6 changes: 6 additions & 0 deletions BilibiliLive/Request/WebRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -705,9 +705,15 @@ struct Replys: Codable, Hashable {
let avatar: String
}

struct Emote: Codable, Hashable {
let text: String
let url: String
}

struct Content: Codable, Hashable {
let message: String
let pictures: [Picture]?
let emote: [String: Emote]?

struct Picture: Codable, Hashable {
let img_src: String
Expand Down

0 comments on commit 4677599

Please sign in to comment.